Public Macros Understanding IF statements
Reply
Understanding IF statements
Wrote this in response to someone today, though I should post it out here publicly. It's a bit of a mess, a rush-job but hopefully helpful.

Something that might help in the reading code is understanding the way conditions are evaluated. So in pseudo code, we have the basic structure of an if.

pseudo
More +
IF (<condition>) THEN
   |does this if condition is true
ELSE
   |does this if condition is false
END IF


So, as the comments suggest, if that <condition> evaluates to TRUE then the first segment gets executed, otherwise the 2nd segment does.

A comparison evaluates to true if it's true and false if it's false, which is fairly simple to understand. So 1 == 1 will evaluate to true, and 1 == 2 will evaluate to false. Practically ${Me.PctHPs}==50 will only evaluate as true when your hit points are at 50%, the rest of the time it evaluates to false and would therefor execute the ELSE segment.

A 0 by itself evaluates to true, (this goes back to those 0/1 settings you saw in that macro), so /if (0) would always evaluate to false and therefor execute the ELSE segment. A null (unset value) will evaluate to false. Any other numeric value evaluates to false. In some languages strings of text evaluate to true but in MQ it will cause an error. So for example ${Me.Casting} returns a null if not casting and if your are casting it returns the name of the spell. So /if (${Me.Casting}) would run the ELSE segment when you aren't casting, but as soon as you cast it will trigger an error because it tries to evaluate text. Any number other than 0 evaluates to true, so something you might see is this instead: ${Me.Casting.ID} which returns a null (evaluates to false) when not casting and the spell ID number (evaluate to true) when you are casting.

The ! operator turns a true into a false and a false into a true. So /if (!${Me.Casting.ID}) would evaluate as true when you aren't casting, it could be read as "If I'm NOT casting then"

There are ORs and ANDs that can be added (||, && respectively) to make a more complex condition. A lot of the time simply reading the whole condition as an english phrase will make it udnerstandable, but sometimes you need to break it down.

/if (!${Me.Sitting} && ${Me.Casting.ID})
or, "if I'm not sitting and I'm casting then do something"

SO when you use an AND the evaluate on both sides of the && must evaluate to true in order for it to run, with an OR only 1 of them needs to evaluate to TRUE. Again, think about it in english, "If I'm sitting or running then shout". You would shout if either of those conditions (sitting or running) was true.

You can string together as many &&, || and conditions as you need. You can also group them by parenthesis and they get evaluated from the inner most parenthetical to the outer most (like math)

(!(1==2) && (1==2 || (1==1 && 2==2)))
(!(1==2) && (1==2 || (TRUE && TRUE)))
(!(1==2) && (1==2 || TRUE))
(!(1==2) && (FALSE || TRUE))
(!FALSE && TRUE)
(TRUE && TRUE)
TRUE

If reading the AND and OR statements as english doesn't help basically with a bunch of ORs only 1 condition has to be true for the whole thing to be true, with a bunch of ANDs every condition must be true for the whole thing to be true.
Sun Aug 11, 2013 1:54 pm
Project Lead
Public Macros Understanding IF statements
Reply