How do I or can I use regular scripts?

Just started using Construct 2? Post your questions here

Post » Mon Aug 31, 2015 7:04 pm

So I am new, and sure this is already somewhere but after awhile searching, cant find it.
I see I can create JS 'Plugins' but what if I just want a JavaScript or something in a particular project?
I am trying to redo a fairly simple script I am using with another engine, to get the same results here, but without scripting it, well, the events system is just slow for me, lots of clicking adding events sub events trying to get things to run in a certain way or order, that I can do in a few mins with a script.
So I am wondering can I use accual scripts at all on a per project basis or do I have to find a means of making them as plug ins that i would really only use in a single project?
A example, I am making a array/grid based layout, 1 layer deep, I make the array, and do all my basic stuff with the code below, but trying to do that in the events system for me is seeming not doable. Maybe for more experienced users with the event system and knowing how to do it properly, but from a scripters view dosent work so well.
Something like this I dont think a plugin would do a good job, as it is set up to work on a particular set of params for a certain project.

Code: Select all
   void Start ()
   {
      MapArray = new ushort[100, 100];
      MapArrayRes = new ushort[100, 100];
      for(int x = 0; x < 100; x++)
      {
         for(int y = 0; y < 100; y++)
         {
            MapArray[x,y] = 0;
         }
      }
      // create random areas of diffrent materials
      int numOfSandAreas = Random.Range(3,50);
      int numOfRockAreas = Random.Range(3,50);
      int numOfStoneAreas = Random.Range(3,50);
      for(int sandX = 0; sandX < numOfSandAreas; sandX++)
      {
         int sizeofarea = Random.Range(1,2);
         int areasize = 0;
         if(sizeofarea == 1) { areasize = Random.Range(0,5); }
         if(sizeofarea == 2) { areasize = Random.Range(5,10); }
         if(areasize > 0)
         {
            int startX = Random.Range(0,100);
            int startY = Random.Range(0,100);
            for(int x = 0; x < areasize; x++)
            {
               for(int y = 0; y < areasize; y++)
               {
                  int tempX = startX + x;
                  int tempY = startY + y;
                  if((tempX < 100) && (tempY < 100)) { MapArray[tempX,tempY] = 2; }
               }
            }
         }
      }
      for(int rockX = 0; rockX < numOfRockAreas; rockX++)
      {
         int sizeofarea = Random.Range(1,2);
         int areasize = 0;
         if(sizeofarea == 1) { areasize = Random.Range(0,5); }
         if(sizeofarea == 2) { areasize = Random.Range(5,10); }
         if(areasize > 0)
         {
            int startX = Random.Range(0,100);
            int startY = Random.Range(0,100);
            for(int x = 0; x < areasize; x++)
            {
               for(int y = 0; y < areasize; y++)
               {
                  int tempX = startX + x;
                  int tempY = startY + y;
                  if((tempX < 100) && (tempY < 100)) { MapArray[tempX,tempY] = 1; }
               }
            }
         }
      }
      for(int stoneX = 0; stoneX < numOfStoneAreas; stoneX++)
      {
         // num 2
         int sizeofarea = Random.Range(1,2);
         int areasize = 0;
         if(sizeofarea == 1) { areasize = Random.Range(0,5); }
         if(sizeofarea == 2) { areasize = Random.Range(5,10); }
         if(areasize > 0)
         {
            int startX = Random.Range(0,100);
            int startY = Random.Range(0,100);
            for(int x = 0; x < areasize; x++)
            {
               for(int y = 0; y < areasize; y++)
               {
                  int tempX = startX + x;
                  int tempY = startY + y;
                  if((tempX < 100) && (tempY < 100)) { MapArray[tempX,tempY] = 3; }
               }
               
            }
         }
      }
      for(int x = 0; x < 100; x++)
      {
         for(int y = 0; y < 100; y++)
         {
// draw the arrays information
         }
      }
B
3
Posts: 2
Reputation: 179

Post » Mon Aug 31, 2015 7:26 pm

If you understand what's going on in a script, and you can't convert that to events then something is terribly wrong.
Image ImageImage
B
168
S
50
G
163
Posts: 8,224
Reputation: 105,067

Post » Mon Aug 31, 2015 7:45 pm

while i can see how to, i also see how much longer it takes to do, instead of quick types i have to right click, add event, double click cond. click add event, double click action, type the parts, rinse and repeat, takes me 2-5mins to write the script, far more time to manual click in events, just assumed there was a means of adding code sniplets or something
B
3
Posts: 2
Reputation: 179

Post » Mon Aug 31, 2015 7:55 pm

If you want to do scripting instead of events,the two options are making a plugin with the sdk or using the run javascript action of the browser object.

Using the sdk is the encouraged method but you can't access other objects in the same way as events. The focus of this engine is the event system, so that is the easiest way to access everything in most cases.

Using the "run javascript" action of the browser object doesn't really have access to anything else in the project. It is mainly used to use some outside library or something. There are ways to access other parts of the engine but it is kind of hacky and will not work if you export with minification.

JavaScript is like the back end of the engine, so apart from the sdk it wasn't meant to be used in snippets in the editor.

That said for a script like that you could do most of it with the "run javascript" action. Everything except the drawing.

The pattern I use is to define functions with a "run javascript" action under a "start of layout" event:
Code: Select all
window.foo = function()
{
   // do stuff
};

"window" is javascript's global object. It's important to define it that way or you won't be able to call it again.
So at that point you can call that function with a string like "foo()" anywhere else.

The next issue is to get the results so you can use it. The browser object also has an "evaluate javascrpt" expression that you can use to get a return value. The only two types that C2 accepts are numbers and text, so you could return the array as text and use some events to parse it so you can use create objects to represent your map in the engine. You could even return a json string that the array object can load, but it would take more code to format it right.
B
91
S
31
G
103
Posts: 5,235
Reputation: 67,756

Post » Mon Aug 31, 2015 8:36 pm

I would bet the speed of script vs event should be about the same, especially given that we have a bunch of keyboard shortcuts, and things like intellisence.
Copy and paste would be way faster I guess.
Image ImageImage
B
168
S
50
G
163
Posts: 8,224
Reputation: 105,067


Return to Beginner's Questions

Who is online

Users browsing this forum: papnikol and 3 guests