Arrays scripting tutorial (Page 4 of 5)

Written by Roguey,

Arrays scripting tutorial, written by Roguey


Adding worth

Now, lets work out how much this fleet cost to make. For this we will need a while loop and a couple of variables. So find your script Count.Ships (if not already high-lighted) and press enter to edit once again. Now insert 1 blank line (at line 1). Now press enter, and select General Commands, then <RetVar/IF><Expression>, <Variable> and finally type in cost. Once back at our script, press enter on <?> and search for <Number>, then enter in 0. We need to set a second variable, so copy (press c and paste (press v). Change the second line to read: $loop = 0. After-which should look like this:

Our Count.Ships script, so far

Now insert a new line at 4 - this is where we will start our while loop. So press enter, General Commands and once again select <RetVar/IF><Expression>. Scroll down and choose while. Now change the <?> into $loop. Press enter on the 3 dots and select the less than sign (ie. <), once again enter on the 3 dots and select ships.count (under Available Variables). You should have something which looks like this:

Our Count.Ships script, so far

So, if you remember we need to do some more code and then complete the loop. So insert a new blank line at line 6. We need to see how much each ship is worth by checking the price of each. This is done by selecting 1 item from our array. We can do this with a simple command, press enter, General Commands, Arrays then add this command (2nd from top):

<RetVar/IF><Var/Array>[<Var/Number>]

Oh.. that looks confusing, eh? Its not. This command allows us to select one item from an array. Add the command and on the script input screen choose <Variable> then key in ship. The first <?>, change to ships (under Available Variables), then the second <?> to loop (also under Available Variables). After your adjustments, you should see this:

Our Count.Ships script, so far

This time we need a command to give us the value of each ship. To do this we need to do two commands: get ware code of object and get average price of ware. This will tell us how much the ship will sell for at a shipyard. So, move to line 7 and insert 2 new blank lines.

So now we need to add both commands, the first being get ware type of object, you can find this command under General Object Commands (around 9 down). This is the one you need:

<RetVar/IF> get ware type code of object

Set the script input to <Variable>, then ship.ware. Once back in our script, set the <?> to ship (under Available Variables). With that done, we need to find out how much this ship would sell at a station (like a shipyard).

So lets find out how much it would sell for. For this move to your second blank line, this time we need find average price of ware. This can be found under Trade, (its near the bottom of the first page) and looks like this:

<RetVar/IF> get average price of <Var/Ware>

Once found, set our script input to ship.price. Now the last <?> (at the end of the line), set to ship.ware in Available Variables. If all done correctly you should have the following:

Our Count.Ships script, so far

So now we know the price of each ship (in the variable called ship.cost) however we need to add them up. Early I told you to create a variable called cost, this where I plan to store this. However we need to keep its current value and add to it. So insert a new line at line number 9, then press enter and head to General Commands. Select <RetVar/IF><Expression>. When ask for a script input, select cost (under Available Variables). Change the <?> to cost (under Available Variables), so we have: $cost = $cost ... . We now need to add the value of the ship, so once you have the 3 dots high-lighted, add a + symbol, followed by the variable ship.cost. You should have the following:

Our Count.Ships script, so far

We now need to increase $loop and end the loop. So insert two blank lines at line 10. On the first line we need to increase the loop variable. So press enter, General Commands and add inc <RetVar> =. Set the script input to Available Variable, loop.

Now we need to complete the loop with a end. So move to the second blank line (at line 11) and press enter. Now goto General Commands, followed by Flow Control. Now add end conditional to our script. We now have a completed loop that works out how much these ships are worth.

Our Count.Ships script, so far

All we need to do now is to add some extra information to our message. We can do this by adding extra details to line 12. So goto line 12 and high-light the 3 dots. Press Enter then add a +. Now add a string, with the text , they are worth: . Next another +, followed by the available Variable cost. One more +, then another string which reads credits.. Our script is now complete, and should look like this:

Our Count.Ships script, so far

Now save the script.

Next: Something wrong?
<< Previous Page - Next Page >>