Common mistakes
EasyUO reports no errors. A line it can’t understand is skipped, and the script goes on. So a mistake shows up only as a script that does the wrong thing, or nothing. This page lists the rules behind most such scripts.
In the examples, a comment that starts with => shows what a variable holds when the script has stopped. These
examples are run through the interpreter every time EasyUO Reforged is tested, so they show what really happens.
The rules that matter most
Section titled “The rules that matter most”- A name that doesn’t exist gives no error. A line that starts with a word that is not a
command is skipped, and a
#variablethat doesn’t exist readsN/A. Take every command and every#variablefrom the lists, never from memory or from another language. - Every word needs spaces around it, operators and parentheses included:
if ( %a > 1 ),set %a %b + 1.(%a > 1)and%b+1are not understood. - Only a few commands calculate:
set,if,while,until,returnandwaitfor. Every other command takes its words as they are:wait %a + 1waits%aticks. Calculate withsetfirst. - True is
-1, false is0. Use#trueand#false.if %flagis true only when%flagholds-1:if 1andif yesare false. - A value is one word.
set %a hello worldstoreshello. Write spaces as_, or glue words with a comma:set %a hello , #spc , world. - A block’s
{is alone on the very next line after its command, and}is alone on its line. Without a block, the command controls only the next line. - End the script with
halt. Reaching the last line starts the script again from the top, andexitin the main script does the same. Onlyhaltandstopstop it. - Put subs after the
halt, where the normal flow never reaches them. Leave a sub withreturn, never withgoto. - A time is in ticks of 50 ms:
wait 20is one second. Or write seconds with ans:wait 5s. Onlysleepcounts in milliseconds. - Game values are your own. Item types (
POF), IDs (YKLMNOP), gump names, journal texts and screen positions differ between shards, clients and characters. Don’t take them from someone else’s script without checking: Finding IDs and types shows how to read yours. - The client holds back speech that comes too fast. Lines sent with
msgor the speech macros past its spam limit arrive late, and so do their answers in the journal. Allow about a second for every 12 characters: see the spam limit.
Each rule in a script:
set %a 1nosuchcommand %a 2 ; rule 1: not a command, so the line is skippedset %v #nosuchvariable ; => %v = N/Aset %b %a + 1 ; => %b = 2set %c %a+1 ; => %c = %a+1halt ; => %a = 1%a+1 in the third set is one word (rule 2), so nothing is replaced and nothing is added.
set %a 2set %right noset %wrong noif ( %a > 1 ) set %right yes ; => %right = yesif (%a > 1) set %wrong yes ; => %wrong = nohaltset %a 5gosub count %a + 1 ; rule 3: gosub doesn't calculate, it passes three wordshalt
sub count set %words %0 ; => %words = 3 set %first %1 ; => %first = 5returnset %x 0if 1 set %x %x + 1 ; rule 4: skipped, 1 is not trueif yes set %x %x + 1 ; skippedif #true set %x %x + 10 ; runsset %t 2 > 1 ; => %t = -1set %f 2 < 1 ; => %f = 0halt ; => %x = 10set %a hello world ; => %a = helloset %b hello_world ; => %b = hello_worldset %c hello , #spc , world ; => %c = hello worldhaltset %n 0if 1 = 2{ set %n %n + 1 ; rule 6: skipped with its block}if 1 = 2
{ set %n %n + 10 ; runs: the blank line was the one line the if controls}halt ; => %n = 10set %runs %runs + 1if %runs < 3 exit ; rule 7: back to the first line, the variables keep their valueshalt ; => %runs = 3Which commands do what
Section titled “Which commands do what”| Commands | |
|---|---|
| Calculate their parameters | set if while until return waitfor |
Take a { } block, or else the next line |
if else for while sub onhotkey cmppix |
Take a time in ticks, or in seconds with s |
wait waitfor findjournal target move path |
- One command per line. The first word is the command, the rest are its parameters.
;starts a comment, anywhere on a line. For a;in a text use#smc, for a.#dot, for a space#spc.- Case doesn’t matter, for commands, variables, sub names and text comparisons.
- A line starting with
+continues the line before it. The pieces join without a space, so write+ morewhere you want one. - A label is a name followed by
:on a line of its own (mainloop:).goto mainloopjumps to it.
set %text abc+defhalt ; => %text = abcdefVariables
Section titled “Variables”- A variable that was never set reads
N/A. - A variable is replaced by its value only when it is a word of its own. Glue it to other text with a comma.
%0,%1,%2, … are the parameters of the lastgosuborcall. They are ordinary variables shared by the whole script, so a sub that calls another sub loses its own. Copy them first.- Numbers are whole numbers.
$1Ais hexadecimal. Text counts as0in arithmetic. %a , %bjoins two values into one word.%item . %ibuilds a variable name: with%iholding2it reads%item2. That is how scripts make lists.
set %a %never ; => %a = N/Aset %count 3set %text gold:%count ; => %text = gold:%countset %glued gold: , %count ; => %glued = gold:3set %hex $1A + 0 ; => %hex = 26set %sum abc + 5 ; => %sum = 5set %item1 swordset %item2 shieldset %i 2set %name %item . %i ; => %name = shieldhaltgosub outer Ahalt
sub outer gosub inner B set %mine %1 ; => %mine = Breturn
sub innerreturnConditions
Section titled “Conditions”=and<>compare as text, ignoring case, when either side is not a number.<><=>=are always false when either side is not a number,N/Aincluded.inandnotintest whether the left text is inside the right one.&&binds tighter than||.!turns around only the value right after it. Writeif ! ( %a = 2 )orif %a <> 2, neverif ! %a = 2./drops the remainder, and dividing by 0 gives0.
set %a helloset %r1 %a = HELLO ; => %r1 = -1set %r2 %a > 33 ; => %r2 = 0set %r3 %a < 33 ; => %r3 = 0set %r4 ell in %a ; => %r4 = -1set %r5 1 = 1 || 1 = 2 && 1 = 3 ; => %r5 = -1set %b 1set %r6 ! ( %b = 2 ) ; => %r6 = -1set %r7 ! %b = 2 ; => %r7 = 0set %r8 7 / 2 ; => %r8 = 3set %r9 7 / 0 ; => %r9 = 0set %r10 2 + 3 * 4 ; => %r10 = 14haltLoops and subs
Section titled “Loops and subs”forcounts down when its start is larger than its end. Sofor %i 1 #FindCntruns twice, for 1 and for 0, whenfinditemfound nothing. Test#FindCnt > 0first.- A
gosubto a sub that doesn’t exist does nothing, and so does acallof a file that isn’t found. return %xputs%xin#Result.- Old scripts write
if %a > 1 2to make theifcontrol the next 2 lines. Read it that way when you meet it, and write blocks in new scripts.
set %runs 0for %i 1 0 set %runs %runs + 1halt ; => %runs = 2gosub add 2 3set %sum #Result ; => %sum = 5set %after beforegosub nosuchsubset %after after ; => %after = afterhalt
sub addreturn %1 + %2