Control structures
A script runs from its first line down. These commands change that order. Each has its own page with the details.
Choosing: if and else
Section titled “Choosing: if and else”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.haltRepeating
Section titled “Repeating”| 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 %iset %n 1while %n < 100 set %n %n * 2display ok %n ; 128haltJumping: labels and goto
Section titled “Jumping: labels and goto”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 0mainloop: set %rounds %rounds + 1 wait 5 if %rounds < 3 goto mainloopdisplay ok %rounds rounds done.haltUse goto within a part of the script, but never to leave a sub: that leaves the sub open.
Subs: sub, gosub and return
Section titled “Subs: sub, gosub and return”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 Jimhalt
sub greet display ok Hello %1return- Parameters after the sub name arrive in
%1,%2, …, and%0holds 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 %valuehands a value back in#Result.- Put subs after the main part of the script, where the normal flow never reaches them; see
subfor why. - To give a sub variables of its own, switch namespace inside it.
Other scripts: call and exit
Section titled “Other scripts: call and exit”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.