if
if() Function
true
, the first expression of code is executed, otherwise the second expression of code is executed.
Important note:
Both trueExpr
and falseExpr
are evaluated regardless of what condition
returns. This means that updates, macro calling, etc. in both branches will be executed regardless of the test result.
So this function should only be used in limited cases where branches stick to returning a value.
However, the function doesn't have the parenthesis limit the roll option has:
[if(((1))): 1;0] <!-- in this case if() roll option fails -->
[if(((1)),1,0)] <!-- in this case if() function works -->
Usage
if(condition, trueExpr, falseExpr)
Parameters
condition
- What is tested to determine is thetrueExpr
orfalseExpr
will be executed. This follows the standard rules for conditions that can be found in the Branching and Looping article.trueExpr
- A section of code that is executed ifcondition
returns astrue
.falseExpr
- A section of code that is executed ifcondition
does not return astrue
.
Examples
a
is 10
and b
is 20
:
[r: if(a > b, "A is larger than B", "A is not larger than B")]
- Returns:
A is not larger than B
Example 2: If number
is 1
:
[r: if(number >= 1, 20, "")]
- Returns: A blank string, please note that a blank string is not an empty variable if you were to assign the output of this function.
Example 3: If variable
is "Foobar"
:
[r: if(variable == "Text", 1, 0)]
- Returns:
0
Example 4: If variable
is 20
[property = if(variable > 0 && variable < 20, 1, 0)]
- Returns:
property
set to0
Example 5: Usually its better to use the roll option version [if():]. Sometimes its pretty handy to use this version - the function-version - since you can easily embed it in loops and expressions.
Lets say you want to check if one of a players tokens has Initiative you could do this like this.
[h: tokensOfPlayer = getOwned(getPlayerName(), "json")]
[h: hasIni = 0]
[h: iniToken = getInitiativeToken()]
[h, foreach(id, tokensOfPlayer): hasIni = if(id!=iniToken,hasIni,1)]