Loops scripting tutorial (Page 1 of 4)

Written by Roguey,

Loops scripting tutorial, written by Roguey


Building a loop

Hopefully now we should have the very basics covered - we can now open up the script editor, write a small script, save and then run it. So the next logical step is making a loop. Loops are important in coding, as they can save you a lot of time from typing the same thing over and over.

Okies... lets make a script to place a satellite in every existing sector. This could be very useful for mods - checking sectors to see whats in them. The first step is to make a new script once again. You need to get the script editor open once again by setting the pilot's name to Thereshallbewings (you dont need to change this each time, just if you start a new game). Once the script editor is activited, then once again open the the script editor:

Ship » Orders » press S » Script editor


ie. You should have something to this on your screen (dont worry if you have different scripts than me - I have my X3 mod installed).

The script editor showing all the current scripts

So lets create a new script called Make.Sats - the dot doesnt do anything, its just there to split up the two words.
We should get another blank script to work with.

Okies move to <New Line> and press enter. This time we need to set two variables: x and y. These variables dont need to be called x and y, it just makes easier to understand what they contain. We need two variables because the universe is laid out in 2D grid: x being left to right, whilst y being up and down. So now select General Commands, then <RetVar/IF><Expression>. What does this do? well.. this allows us to set something or make a If statement. At the moment we simply want to set two variables x and y to zero. So add this to our code (by pressing enter once again), we next see this screen:

Inputs for our <RetVar/IF><Expression> command

So select <variable> and write in x. We should be back at our script with $x = <?>. So we need to change that <?> to the number zero. So press enter, and scroll down the input list until you reach <Number> (this is just after null in the script input selection). Then press enter and write in the number 0. So now we should have the following:

X equals zero

Now we need to do this for y however we have two ways to do this: we could copy and paste (then change x to y) or do the above again... although thats not really learning is it?.. lets copy and paste! So make sure 001 is selected and then press c to copy this line. Now to paste it press v. You should have something like this:

X equals zero.. twice!

So we need to change the second $x to $y. This can be done by moving your high-lighted cursor to the second $x. Now press enter and move down to <variable>.

Changing the variable x to y

Now press enter and type in y. Your script should now look like this:

Our script with x and y set to zero

Next: Building a loop
Next Page >>