If…Then
Everything in TorqueScript is strings!
This is important to remember with if statements, because in if statements you can either compare by int or by string.
To compare by string you should write $= instead of ==.
Example:
The general syntax for if-statements is still very similar to C though:
Switch
As with if-statements, switch statement has slightly different syntax, based on whether you want to evaluate a numeric or a string expression.
For numeric values, you just use switch(…), but for string values you use switch$(…) instead.
Example:
While Loop
While loops are identical to C, i.e. you just have while(…) and nothing else.
For Loop
A for loop is a loop that usually contains a pre-defined number of how many iterations the loop should run.
The loop will run until the condition evaluates to false.
The basic syntax in a for loop in TorqueScript is as follows:
An actual code example would look like this:
For Each
There is also a foreach loop that exists in TorqueScript. For each loops are like for loops but with briefer and clearer syntax. There are two kinds of foreach loop: one that works on iterable objects like SimSet and SimGroup, and one that works on strings.
An example of the former is:
And of the latter: