if: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
(Emphasising usage differences between if function and if roll option)
No edit summary
 
(8 intermediate revisions by 5 users not shown)
Line 3: Line 3:
|version=1.3b38
|version=1.3b38
|description=
|description=
This function is used to check whether a certain code ''expression'' should be executed or not. If the ''condition'' to be evaluated with this function is {{code|true}}, the first ''expression'' of code is executed, otherwise the second ''expression'' of code is executed.  
Is used to check whether a certain code ''expression'' should be executed or not. If the ''condition'' to be evaluated with this function is {{true}}, the first ''expression'' of code is the result, otherwise the second ''expression'' of code is the result.


{{note|Note that both ''expressions'', the true and the false, are evaluated!
This means that updates, macro calling, etc. in '''both''' ''expressions'' will be executed regardless of the test result.<br />
Therefore, this function should only be used in limited cases where the ''expressions'' are simply returning a value.}}


<span style="color:red;font-size:1.1em">'''<u>Important note:</u>'''</span><br>
This function doesn't have the parentheses limit that the roll option has:
 
<syntaxhighlight lang="mtmacro">
Both {{code|trueExpr}} and {{code|falseExpr}} are evaluated regardless of what {{code|condition}} returns. This means that updates, macro calling, etc. in '''both''' ''branches'' will be executed regardless of the test result.<br>
[if(((1))): 1;0]     <!-- in this case if() roll option fails -->
So this function should only be used in limited cases where ''branches'' stick to returning a value.
[if(((1)),1,0)]     <!-- in this case if() function    works -->
 
</syntaxhighlight>
However, the function doesn't have the parenthesis limit the roll option has:
<source lang="mtmacro">
[if(((1))): 1;0] <!-- this fails -->
[if(((1)),1,0)] <!-- this works -->
</source>
|usage=
|usage=
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
if(condition, trueExpr, falseExpr)
if(condition, trueExpr, falseExpr)
</source>
</syntaxhighlight>
'''Parameters'''
'''Parameters'''
* {{code|condition}} - What is tested to determine is the {{code|trueExpr}} or {{code|falseExpr}} will be executed. This follows the standard rules for ''conditions'' that can be found in the [[Macros:Branching and Looping|Branching and Looping]] article.
* {{code|condition}} - What is tested to determine is the {{code|trueExpr}} or {{code|falseExpr}} will be executed. This follows the standard rules for ''conditions'' that can be found in the [[Macros:Branching and Looping|Branching and Looping]] article.
* {{code|trueExpr}} - A section of code that is executed if {{code|condition}} returns as {{code|true}}.
* {{code|trueExpr}} - A section of code that is returned if {{code|condition}} is {{true}}.
* {{code|falseExpr}} - A section of code that is executed if {{code|condition}} does not return as {{code|true}}.
* {{code|falseExpr}} - A section of code that is returned if {{code|condition}} is {{false}}.
 
&nbsp;
<!-- The 'nbsp' is needed to force the DIV to close. -->
|examples=
|examples=
'''Example 1:''' If {{code|a}} is {{code|10}} and {{code|b}} is {{code|20}}:
'''Example 1:'''
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[h: a = 10] [h: b = 20]
[r: if(a > b, "A is larger than B", "A is not larger than B")]
[r: if(a > b, "A is larger than B", "A is not larger than B")]
</source>
</syntaxhighlight>
'''Returns:''' {{code|A is not larger than B}}
:'''Returns:''' {{code|A is not larger than B}}


'''Example 2:''' If {{code|number}} is {{code|1}}:
 
<source lang="mtmacro" line>
'''Example 2:'''
<syntaxhighlight lang="mtmacro" line>
[h: number = 1]
[r: if(number >= 1, 20, "")]
[r: if(number >= 1, 20, "")]
</source>
</syntaxhighlight>
'''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.
:'''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 {{code|variable}} is {{code|"Foobar"}}:
 
<source lang="mtmacro" line>
'''Example 3:'''
<syntaxhighlight lang="mtmacro" line>
[h: variable = "Foobar"]
[r: if(variable == "Text", 1, 0)]
[r: if(variable == "Text", 1, 0)]
</source>
</syntaxhighlight>
'''Returns:''' {{code|0}}
:'''Returns:''' {{code|0}}


'''Example 4:''' If {{code|variable}} is {{code|20}}
 
<source lang="mtmacro" line>
'''Example 4:'''
<syntaxhighlight lang="mtmacro" line>
[h: variable = 20]
[property = if(variable > 0 && variable < 20, 1, 0)]
[property = if(variable > 0 && variable < 20, 1, 0)]
</source>
</syntaxhighlight>
'''Returns:''' {{code|property}} set to {{code|0}}
:'''Returns:''' {{code|property}} set to {{code|0}}
 
 
'''Example 5:''' Usually its better to use the roll option version {{roll|if}}. Sometimes it's pretty handy to use the version documented here, since you can easily embed it in loops and expressions.
 
Let's say you want to check if one of a player's tokens has Initiative, you could do it this like this:
<syntaxhighlight lang="mtmacro" line>
[h: tokensOfPlayer = getOwned(getPlayerName(), "json")]
[h: hasIni = 0]
[h: iniToken = getInitiativeToken()]
[h, foreach(id, tokensOfPlayer): hasIni = if(id!=iniToken,hasIni,1)]
</syntaxhighlight>


|also=
|also=

Latest revision as of 16:22, 15 March 2023

if() Function

Introduced in version 1.3b38
Is used to check whether a certain code expression should be executed or not. If the condition to be evaluated with this function is true(1), the first expression of code is the result, otherwise the second expression of code is the result.


Note that both expressions, the true and the false, are evaluated!

This means that updates, macro calling, etc. in both expressions will be executed regardless of the test result.

Therefore, this function should only be used in limited cases where the expressions are simply returning a value.


This function doesn't have the parentheses limit that 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 the trueExpr or falseExpr 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 returned if condition is true(1).
  • falseExpr - A section of code that is returned if condition is false(0).

 

Examples

Example 1:
[h: a = 10] [h: b = 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:

[h: number = 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:

[h: variable = "Foobar"]
[r: if(variable == "Text", 1, 0)]
Returns: 0


Example 4:

[h: variable = 20]
[property = if(variable > 0 && variable < 20, 1, 0)]
Returns: property set to 0


Example 5: Usually its better to use the roll option version [if():]. Sometimes it's pretty handy to use the version documented here, since you can easily embed it in loops and expressions.

Let's say you want to check if one of a player's tokens has Initiative, you could do it this like this:

[h: tokensOfPlayer = getOwned(getPlayerName(), "json")]
[h: hasIni = 0]
[h: iniToken = getInitiativeToken()]
[h, foreach(id, tokensOfPlayer): hasIni = if(id!=iniToken,hasIni,1)]

See Also