S cripting - III. Variables
Let's take a look at variables. They are a kind of container for values. Stranded II Script can only save Integer values, respectively natural numbers, to variables. Variables are branded by a Dollar sign (\$).
Let us take the code we just worked on and implement a variable:
```on:start {
\$myvalue=5;
msg "Hello World! \$myvalue";
}
```

If we now test the script, we can state that "Hello World! 5" is put out. What have we done?
Easy to explain: Bloody nonsense. This script is total rubbish. But that's irrelevant. By "\$myvalue=5;", we have declared a new variable and given it the value 5 via equal sign. This assignment is, just like commands, finalised by a semicolon. We have included the variable \$myvalue into the parameter of the command "msg". The Dollar sign tells Stranded II that it is dealing with a variable and automatically replaces the variable's name by the value it contains. This is, strongly simplified, how variables work.
Now let us continue to change the code:

```on:start {
\$myvalue=5;
msg "Beforehands: \$myvalue";
\$myvalue=\$myvalue*2;
msg "Afterwards: \$myvalue";
}
```

We can state: Calculations with variables are possible, too. Since the variable gets replaced by the value, Stranded II calculates 5*2 and afterwards saves the result (10) in the variable.
Apart from the normal assignment via equal sign, you can also increase or decrease a variable directly (if it does not exist yet, it will be set to 0 first and then increased or decreased):
++ - increases the value by 1
+= - increases the value by a given value
-- - decreases the value by 1
-= - decreases the value by a given value
Here is a little code for it to illustrate:
```on:start {
//Set variable to ten (-> 10)
\$var=10;
//Decrease variable by seven (-> 7)
\$var-=3;
//Increase variable by one (-> 8)
\$var++;
//Increase variable by itself (-> 16)
\$var+=\$var;
//Output
msg \$var;
}
```

The lines that start with "//" are comments. All text within the line that is behind the "//" will be ignored. It is recommendable to write comments into the scripts from time to tome, so that you get along better and know immediately what the script does at the particular place again. You can also build in comments over several lines. They are initiated with "/*" and end with "*/".
Another to the former scripts is that we now just hand the variable to the msg-command without any additional text.
It would of course be possible as well to, for instance, increase one variable by another (\$odin+=\$thor) and much more.But to show you all that here would extend the size of this tutorial - and is not necessary at all. Just try around a bit.

Local and global

Until now, we have just made our variables by writing \$x=y. But variables that are created in that way are always global - automatically! That means: Any script has access to them. No matter where it is executed. Under special circumstances, this can, however, be undesirable. For instance, if you want to count for each object, seperately, how often the player hits it. A little experiment shall illustrate this:

• Open the file "objects_palms.inf" in the folder "mods\Stranded II\sys"
• Insert the following script directly under the line "script=start":
```on:hit {
\$hits++;
msg "Hits on the palm: \$hits";
}
```

• Save the file (we hereby just changed - resp.: created - a definition script!)
• Start Stranded II (If it is already running, you must quit and restart it!)
• Create a map with several palms (palms of type 1, of course!) and test it.
If you now hit a palm (point at it and left-click to attack), "Hits on the palm: X" will be displayed, and X will increase with every strike. If you now continue at another palm, X will just be increased further instead of beginning again at 0 for that palm. This is not what we desired in this. We want a seperate counter for the number of hits on each palm.
The solution are local varials. In contrast to global varials, access to them can only be gained in scripts of the object where they are definded as local (except if one uses the command setlocal or getlocal).

• The command "local" in the command reference

• Open the file "objects_palms.inf" in the folder "mods\Stranded II\sys"
• Insert the following line below the line on:hit {, that we have just written:
```local \$hits;
```

• Repeat the previous steps (Save, Creat map, Test)
And already, it works. For each palm there is an own counter now. Only thanks to local variables.
Attention: Do not forget to undo the changes in the "objects_palms.inf" after testing, or that is to say: remove the script you added.
That actually is all concerning local and global varials. By the way: All variables are saved with the savegames. Exept if you...

Temporary variables

..yes, except if you mark them as temporary variables. Another little speciality of Stranded II Script.
By the command "temp", you tell the game to treat a variable as temporary. It will then behave just like a normal variable, but with the difference that it will be automatically deleted before saving the map. With "tempall", all variables will instantly become temporary variables. Of course, global and local variables can both be temporary.

• The command "temp" in the command reference
• The command "tempall" in the command reference

Commands with output

This topic may actually not really belong to variables, but it will be broached here as well because commands that give back output values behave just like variables.
If you execute a command in Stranded II that gives back an output, you must keep in mind that there is a little difference to normal runs of commands here: The parameters must be set into round brackets. If there are no parameters to specify, just an empty pair of curved brackets is to be attached. The script will then replace the command with output by its output value - just like the name of a normal variable gets replaced by the value of the variable.
Voila, an example:
```on:start {
msg gt();
msg random(100);
}
```

All commands with output values are sorted into an extra category of the command reference:

• Return-commands in the command reference

Summary

Stranded II Script includes local and global variables which are branded by a Dollar sign. The variables are saved into savegames, except if they are marked as temporary.
Commands that return something are executed with parameters in brackets, or with an empty pair of round brackets after the command, if they have no parameters.

• IV. Conditions
Stranded I, Stranded II & Stranded III are games by Unreal Software 2003-2023 | Contact | Disclaimer