Skip to content

Control structures

A script runs from its first line down. These commands change that order. Each has its own page with the details.

if runs a line or block only when a condition is true; else runs another when it is false. Only #true (-1) counts as true.

if #Hits < 50
display ok Heal first.
else
display ok Ready to fight.
halt
Loop Use it to
for Count through numbers.
while Repeat while a condition holds; test first.
repeat … until Repeat until a condition holds; test after each round.

break leaves a loop; continue skips to its next round.

for %i 1 3
display ok Round %i
set %n 1
while %n < 100
set %n %n * 2
display ok %n ; 128
halt

A label is a name followed by a colon, on a line of its own. goto jumps to it. It is the simplest way to build the main loop of a script:

set %rounds 0
mainloop:
set %rounds %rounds + 1
wait 5
if %rounds < 3
goto mainloop
display ok %rounds rounds done.
halt

Use goto within a part of the script, but never to leave a sub: that leaves the sub open.

A sub is a named group of lines ending with return. gosub runs it and comes back to the line after the gosub. Subs keep scripts short and readable: write something once, use it from many places.

gosub greet Jim
halt
sub greet
display ok Hello %1
return
  • Parameters after the sub name arrive in %1, %2, …, and %0 holds how many there are. These are ordinary variables shared by the whole script, so a sub that calls another sub loses its own %1, %2; copy them into variables of their own first.
  • return %value hands a value back in #Result.
  • Put subs after the main part of the script, where the normal flow never reaches them; see sub for why.
  • To give a sub variables of its own, switch namespace inside it.

call runs another script file and comes back when it ends or reaches exit. In the main script, exit and reaching the last line both start the script over; halt stops it.

Based on the EasyUO wiki, edited for EasyUO Reforged. This text is available under the GNU Free Documentation License 1.2.