Flow Control
MQ2 provides a few ways to control the execution of your macro.
  • If/else decisions
  • While loops
  • For loops
  • Goto jumps

If/else decisions

  • If statement blocks are the most common form of flow control in MQ2.
  • The syntax for an /if statement can be found here.
  • The simplest statements are one-liners like the following:
    1
    /if (${Me.Sitting}) /stand
    Copied!
    This will stand you up if you're sitting.
*One-line /if statements do not require curly braces ( {} )
  • If you wish to execute more than one command, you must wrap your set of commands in braces:
1
/if (${Me.Sitting}) {
2
/stand
3
/echo I'm standing now!
4
}
Copied!
*If statements can also match multiple conditions for fine-grained control:
1
/if (${Me.Standing} && !${Me.Combat} && !${Me.Casting.ID}) {
2
/sit
3
/echo Medding now
4
}
Copied!
The above example will only be matched if you're currently standing and you're not in combat and you're not casting. Any of the Operators can be used to "fine-tune" the matching of any conditions. Also, brackets can be used to group certain sets of statements together.
1
/if ( (${Me.Sitting} && !${Window[SpellBookWnd].Open}) || ${Me.Standing} ) /call Cast "Complete Healing" 10s
Copied!
This will only cast the spell if:
  • I'm sitting and the spellbook window is not open
OR
  • I'm standing.
You will also need the curly braces any time you're using else or else if statements. An example:
1
/if (${Me.Sitting}) {
2
/stand
3
/echo I'm standing now!
4
} else {
5
/echo I'm already standing
6
}
Copied!
Else blocks are extremely useful for catching "everything else" that is not matched by the /if statement.
You can go one further and add some further matching with additional /if statments, like the following:
1
/declare random int local ${Math.Rand[10]}
2
/if (${random}<=3) {
3
/echo The number is less than or equal to 3
4
} else /if (${random}<=5) {
5
/echo The number is less than or equal to 5
6
} else /if (${random}<=8) {
7
/echo The number is less than or equal to 8
8
} else {
9
/echo The number is 9 or higher
10
}
Copied!
You can also nest /if statements within other /if statements, to make your matching and macro flow very powerful.

While loops

Similar to for loops, while loops can be used to execute a series of commands as long as a certain set of conditions are or are not true.
1
/while (${Me.PctHPs}<100 || ${Me.PctMana}<100) {
2
/if (${Me.Standing}) {
3
/echo Sitting because I am standing.
4
/sit
5
}
6
/echo Resting until my hitpoints (${Me.PctHPs}%) and mana (${Me.PctMana}%) are at 100%.
7
/delay 6s
8
}
Copied!
You can end a /while loop immediately with /break or try the next iteration with /continue

See also /while

For loops

For loops are used when you want to perform the same set of commands on a list of items. With a /for loop, you define a starting number, an end number and (if necessary) a number to increment by. The loop will perform the set of commands with the starting number, then will increment it (by 1 as a default, unless you specify the number) and will then re-run all the commands with the next number. It will continue to do that until it reaches the end number, at which time it will exit.
Take this example, which will give a list of all your buff slots:
1
/declare x int local
2
/for x 1 to 15
3
/echo Buff ${x} is ${Me.Buff[${x}]}
4
/next x
Copied!
The problem with this example is that you'll get a couple of NULLs if you have no buffs in those slots, so we can further refine the loop to make use of an /if statement as well, like this:
1
/declare x int local
2
/for x 1 to 15
3
/if (${Me.Buff[${x}].ID}) /echo Buff ${x} is ${Me.Buff[${x}]}
4
/next x
Copied!
This will have a look and see if the buff in slot ${x} has an ID (remember that /if statements only deal with numbers) and if the number is positive (ie. not 0), then it will echo the buff name.
You could easily list the buffs in reverse order by using "downto" instead of "to" in the /for loop.
1
/declare x int local
2
/for x 15 downto 1
3
/if (${Me.Buff[${x}].ID}) /echo Buff ${x} is ${Me.Buff[${x}]}
4
/next x
Copied!
You can end a /for loop immediately with /break or try the next iteration with /continue.

See also /for

Goto jumps

As with most programming and scripting languages, the use of the "goto" function is frowned upon (generally because it's slow and there are better ways of achieving the same result). In MQ2 it's not much different, and since MQ2 originally didn't include any type of "while" function, many older macros still include goto statements.
First off, you need a marker to denote where the /goto function should move to. A marker is created in the following format:
1
:marker
Copied!
To utilize that marker with a /goto statment, do something like the following:
1
Sub Main
2
/echo start here
3
4
:marker
5
/echo just passed the marker
6
/goto :marker
7
/return
Copied!
In the above example, your MQ2 chat window would be filled with "just passed the marker" echoed constantly until you ended the macro. We've created an infinite loop above, which is the most common use of /goto. A conditional /goto (ie. a /goto after an /if statment) is the closest you can get to a "while" function. Take the following example:
1
Sub Main
2
/declare num int local 1
3
:marker
4
/echo Number is ${num}
5
/varcalc ${num} ${num}+1
6
/if (${num}<=5) /goto :marker
7
/return
Copied!
This would output:
1
[MQ2] Number is 1
2
[MQ2] Number is 2
3
[MQ2] Number is 3
4
[MQ2] Number is 4
5
[MQ2] Number is 5
Copied!
The macro echoes "${num}" and then increments it by one. If the total is less than or equal to 5, it loops back to the start. If the total is above 5, it won't loop back and will end naturally.

:OnExit

Anything included after that label will be called whenever an /endmacro command is issued. To use this feature, the label must be at the end of your 'Sub Main' function and end with a /return. Please note that this is NOT required of macros, so no macros will have to be altered unless you wish to take advantage of this feature.
OnExit will not function upon a macro ending while paused.
example:
1
Sub Main
2
/while (1) {
3
/delay 5
4
}
5
/return
6
:OnExit
7
/echo Should run on exit.
8
/return
Copied!
Last modified 4mo ago