if (roll option): Difference between revisions
No edit summary |
m (Typographical tweaks) |
||
Line 13: | Line 13: | ||
</source> | </source> | ||
'''Parameters''' | '''Parameters''' | ||
{{param|condition|The condition/s to check to determine which roll({{code|true_body}} or {{code|false_body}}) is executed, if any. The condition/s can only contain one level of nested parenthesis.}} | {{param|condition|The condition/s to check to determine which roll ({{code|true_body}} or {{code|false_body}}) is executed, if any. The condition/s can only contain one level of nested parenthesis.}} | ||
{{param|true_body|The roll that is executed if the {{code|condition}} evaluates to {{true}}. To use complex rolls in the {{code|true_body}}, you must use the {{roll|code}} roll option in conjunction with this roll option.}} | {{param|true_body|The roll that is executed if the {{code|condition}} evaluates to {{true}}. To use complex rolls in the {{code|true_body}}, you must use the {{roll|code}} roll option in conjunction with this roll option.}} | ||
{{param|false_body|The roll that is executed if the {{code|condition}} evaluates to {{false}}. If no {{code|false_body}} is given, there is no output if the {{code|condition}} evaluates to {{false}}. To use complex rolls in the {{code|false_body}}, you must use the {{roll|code}} roll option in conjunction with this roll option.}} | {{param|false_body|The roll that is executed if the {{code|condition}} evaluates to {{false}}. If no {{code|false_body}} is given, there is no output if the {{code|condition}} evaluates to {{false}}. To use complex rolls in the {{code|false_body}}, you must use the {{roll|code}} roll option in conjunction with this roll option.}} | ||
Line 33: | Line 33: | ||
* {{code|false}} | * {{code|false}} | ||
* {{code|!}} - Not | * {{code|!}} - Not | ||
It is important to note that the ''Equal to'' condition operator must be two equal signs({{code|{{=}}{{=}}}}). If you are checking for a text string, place quotes around the text. | It is important to note that the ''Equal to'' condition operator must be two equal signs ({{code|{{=}}{{=}}}}). If you are checking for a text string, place quotes around the text. | ||
'''Help! There are ' ' in the | '''Help! There are ' ' in the output…''' | ||
Note that currently <source lang="mtmacro">[r,if(val == something),CODE:{Print something}]</source> will produce extraneous single quotes in the output when the condition is false. The workaround for this is to add an empty block for the false side: <source lang="mtmacro">[r,if(val == something),CODE:{Print something};{}]</source> | Note that currently <source lang="mtmacro">[r,if(val == something),CODE:{Print something}]</source> will produce extraneous single quotes in the output when the condition is false. The workaround for this is to add an empty block for the false side: <source lang="mtmacro">[r,if(val == something),CODE:{Print something};{}]</source> | ||
Line 52: | Line 52: | ||
</source> | </source> | ||
These examples perform the same function. If val is not a number, make val equal 0. | These examples perform the same function. If {{code|val}} is not a number, make {{code|val}} equal {{code|0}}. | ||
<source lang="mtmacro" line> | <source lang="mtmacro" line> | ||
[h, if (! isNumber(val)): val = 0)] | [h, if (! isNumber(val)): val = 0)] |
Revision as of 11:36, 13 March 2012
[if():] Roll Option
Branches the flow of the roll as determined by the condition.
Usage
[if(condition): true_body]
[if(condition): true_body; false_body]
Parameters
condition
- The condition/s to check to determine which roll (true_body
orfalse_body
) is executed, if any. The condition/s can only contain one level of nested parenthesis.true_body
- The roll that is executed if thecondition
evaluates totrue
(1
). To use complex rolls in thetrue_body
, you must use the [code():] roll option in conjunction with this roll option.false_body
- The roll that is executed if thecondition
evaluates tofalse
(0
). If nofalse_body
is given, there is no output if thecondition
evaluates tofalse
(0
). To use complex rolls in thefalse_body
, you must use the [code():] roll option in conjunction with this roll option.
Operators
Operators are used to compare two variables, strings, literal numbers, expressions, or function outputs within a condition
.
Conditional Operators:
>
- Greater than<
- Less than>=
- Greater than or equal to<=
- Less than or equal to==
- Equal to!=
orne
- Not equal
Logical Operators:
&&
- And||
- Or
Boolean Operators:
true
false
!
- Not
It is important to note that the Equal to condition operator must be two equal signs (==
). If you are checking for a text string, place quotes around the text.
Help! There are ' ' in the output…
Note that currently
[r,if(val == something),CODE:{Print something}]
will produce extraneous single quotes in the output when the condition is false. The workaround for this is to add an empty block for the false side:
[r,if(val == something),CODE:{Print something};{}]
Example
Sets the variable newVal
to 12*12
if the variable val
equals 12
.
[h:val=12]
[h,if(val == 12): newVal=12*12]
New Value = [r: newVal]
Returns New Value = 144
Example with logical operators:
[h,if((val > 12 && val < 24) || val == 5): val=1 ; val=0]
These examples perform the same function. If val
is not a number, make val
equal 0
.
[h, if (! isNumber(val)): val = 0)]
[h, if (isNumber(val) == 0): val = 0)]
[h, if (isNumber(val) == false): val = 0)]
The following will generate an error:
[h,if(getName(getSelected()) == "Giant Rat"): val=1] --- ERROR, too many parenthesis on condition!
[h,if(getName() == "Giant Rat")): val=1] --- This is OK.
See Also
if(), isNumber(), [code():], Introduction to Macro Branching