Sanny Builder
Search…
Loops
A loop is a statement which allows code to be repeatedly executed. Sanny Builder supports three kinds of controls loops: for, while, repeat.

FOR..END

The FOR loop has a strictly certain number of iterations (repetitions).
Syntax: FOR <loop variable> = <initial value> TO/DOWNTO <final value> [step = 1] <the loop body> END
<loop variable> - a variable used as a counter for iterations <initial value> - a value of the loop variable before the first iteration (any value including a model identifier) TO or DOWNTO - increment or decrement the loop variable between iterations <final value> - a value of the loop variable after the last iteration (any value including a model identifier) <step> - an optional value the loop variable will be incremented or decremented with between iterations. By default it is equal to 1.
1
var
2
$value: int = 0
3
$final: int = 100
4
end
5
6
FOR $MyCounter = 1 to $final step 2
7
$value += $mycounter
8
end
Copied!
If the loop variable is not declared with any type before the loop it gets the Integer type. If initial value, final value or step are variables, they get the same type the loop variable has To use floating-point numbers for the initial and final values, declare the loop variable with the Float type.
1
var
2
$MyCounter: float
3
end
4
5
FOR $MyCounter = 1.0 to $final step 2.0
6
end
Copied!
Variables $MyCounter and $final both have the Float type after the loop.

WHILE..END

Syntax: WHILE <loop condition> <the loop body> END
loop condition - a single conditional opcode loop body - commands to execute on each iteration; can be omitted
1
while not #AK47.Available
2
wait 0
3
end
Copied!
The WHILE loop works while the loop condition is true. The condition is evaluated before a loop iteration. Hence, if the condition is false, the loop body never gets executed.
1
$var = 10
2
3
while $var > 11
4
inc($var)
5
end
6
7
// as the loop condition is false, inc($var) never gets executed
Copied!
Constants True and False can be used as a loop condition.
1
while true
2
<loop body>
3
end
Copied!
This loop body executes infinitely until the loop is stopped with the Break command.
1
while false
2
<loop body>
3
end
Copied!
This loop is ignored by the compiler as the condition is never met.
Currently the compiler accepts only one opcode in the loop condition, but you can check more conditions before the loop body and use the commands Break and Continue.
1
while true
2
if and
3
$var >= 0
4
$var <= 100
5
then
6
Break
7
end
8
// loop executes while $var is in the range [0...100]
9
10
<loop body>
11
end
Copied!

REPEAT..UNTIL

Syntax: REPEAT <the loop body> UNTIL <loop condition>
loop body - commands to execute on each iteration; can be omitted loop condition - a single conditional opcode
The REPEAT..UNTIL loop executes until the loop condition returns false. The condition is evaluated after iteration therefore the loop is guaranteed to be executed at least once.
Constants True and False can be used as the loop condition.
1
repeat
2
// the loop has the only iteration
3
until true
Copied!
1
repeat
2
// the loop executes infinitely until it's stopped with the Break command
3
until false
Copied!
Currently the compiler accepts only one opcode in the loop condition, but you can check more conditions after the loop body and use the commands Break and Continue.
1
repeat
2
<loop body>
3
if and
4
$var >= 0
5
$var <= 100
6
then
7
Break
8
end
9
// loop executes while $var is in the range [0...100]
10
until false
Copied!

Continue and Break

If you want to skip the current iteration and proceed to the next one, use the Continue command.
The Break command causes the loop to stop immediately and proceed to the command after the loop body.
They can substitute an opcode parameter (e.g., jf Continue) or serve as a standalone statement.
1
while true
2
if
3
not $currentactor.dead
4
jf Break // exit the loop
5
6
if
7
$currentactor.dead
8
then
9
Continue // go to the next iteration
10
end
11
end
Copied!
Last modified 1yr ago