Rûn Statements and Control Flow
How a script decides, repeats and stops, in event scripts and in programs.
Event scripts
A section runs its lines from the top. if, else, while and repeat own the lines indented under them. Braces may stand in for the indents: if (x) {, } else {, } else if (y) { and a closing }, each on its own line.
if <condition>
<lines that run when it is true>
else
<lines that run when it is false>
while <condition>
<lines that repeat while it is true>
repeat <count>
<lines that run count times>[use]
add prayers 1
if prayers >= 3
flag set shrine_blessed
else
set left 3 - prayers
repeat 2
add candles 1
set n 0
while n < 4
add n 1Other lines change the order:
- require flag <name> : <section>
- Go on only when the flag is set; otherwise jump to that section.
- require <attribute> <dc> : <section>
- A hidden d20 roll plus the attribute's modifier against the number; falling short jumps.
- end
- Stop this section here.
- run <name> [section]
- Run another script's section (
[use]when none is named), then carry on here: an event script of that name, or a program when there is none. - wait <seconds>
- Pause this section; the world goes on.
wait 0.5is half a second. - every <seconds>
- Run this whole section again on that beat while the map is loaded (0.25 seconds at the shortest).
[use]
require flag has_gate_key : locked
flag set gate_open
end
[locked]
set tried_gate 1A section holds at most 64 lines (a loop may run them many times); lines past the 64th are not run, so split a long scene into sections and run them. Calls nest at most 16 deep, and a call to a section that is already running (a section calling itself, or two calling each other in a ring) is not made: the console says so. A section that runs 20000 lines in one frame (a while whose test never turns false) is stopped there.
A tab indents a line as spaces do: it reaches the next multiple of four columns, so a tab-indented line is inside the if above it.
Programs
if and else
The condition stands in brackets. The body is one statement, or several in braces. else if chains as in C and JavaScript. In a condition a single = compares, as == does, and a single & or | means && or ||; a line such as hp = 5 on its own still sets.
number hp = 24
if (hp <= 0) {
print("down")
} else if (hp < 25) {
print("badly hurt")
} else {
print("standing")
}
if (hp > 10) print("one line needs no braces")badly hurt
one line needs no braceswhile
number torches = 0
while (torches < 3) {
torches = torches + 1
}
print("lit", torches)lit 3for
for (start; test; step), as in C. The start is i = 0 or number i = 0; the step is an assignment such as i = i + 1, or nothing. A variable the start declares lives only inside the loop.
for (number i = 1; i <= 3; i = i + 1) {
print("wave", i)
}wave 1
wave 2
wave 3return
return ends a function, giving back the value after it if there is one. Outside a function it ends the run.
print("before")
return
print("never printed")beforeWhat is not there
Programs have no break, continue, switch, +=, -= or ++. Write i = i + 1. To leave a loop early, make its test false, or put the loop in a function and return from it.
FirstOver(number limit) {
for (number i = 0; i < 100; i = i + 1) {
if (i * i > limit) return i
}
return -1
}
print(FirstOver(50))8number coins = 1
coins += 2expected a value hereLoops that never end
A while or for that goes round 100000 times stops the program with an error, and nothing after it runs. A loop in a program cannot wait for the world to change, because a program runs to its end within one frame; to act over time, use on update (see Rûn Events: Where Scripts Run).
number n = 0
while (true) {
n = n + 1
}this while ran 100000 laps