7/21/08

IV: if then else

if then else is a Lua function. The syntax format is if <condition> then <functions to be done if condition is right> else <function to be done if the condition is not right> end



Relational Operators


== means "is equal to" Note: it doesn't mean x will be 1 in x==1 it is only used to compare if "x is equal to 1"
~= means "is not equal to"
> means "greater than"
<= means "less than or equal to "
>= "greater than or equal to "


determining if you have full HP
/run p="Player" p=UnitHealth(p)*100/UnitHealthMax(p) if p==100 then SendChatMessage("I Have Full HP") else SendChatMessage("My Hp isn't full") end
Note: you don't have to make the "else" part if you don't have anything to put there, instead of adding it, just make if then end

Determining if variable is not nil


you can make a condition in if statement that will only allow the command run only if the variable has a value: like
/run if y then SendChatMessage("Variable Y contains this value: "..y) else SendChatmessage("VariableY doesn't have any value") end
this will say the else code "VariableY doesn't have any value" if y has no value

Using Functions


Functions like UnitIsDead(), UnitIsPlayer(), UnitIsVisible() returns nil if false and return 1 if true, therefore it is same as example above
/run if UnitIsDead("player") then message("you're dead") else SendChatMessage("You're alive") end
Note: always remember to add end after else or then

Adding more than one condition

Adding more than one condition on the script will require you to use logical operators:


Logical Operators



and -- Makes the code execute only if both of the condition is true
or -- Makes the code execute if one of the condition is true
not -- Makes the code execute if none of the condition is true

Logical Operator: And



/run if UnitIsDead("target") and UnitIsPlayer("target") and UnitCanAssist("target","player") then message(UnitName("target").." is dead, and is an allied player") end

Logical Operator: OR



/stopmacro [noharm]
/run if (UnitCreatureType("target") == "Humanoid") or (UnitCreatureType("target") == "Beast") then SendChatMessage("Casting Polymorph on %t! stop attacking!") end
/cast polymorph



No comments: