if: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
No edit summary
m (Tweeaking page layout and deleting hidden text (wrong template MacroFunction usage))
Line 30: Line 30:
[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>
</source>
'''Returns:''' {{code|A is not larger than B}}
:'''Returns:''' {{code|A is not larger than B}}
 


'''Example 2:''' If {{code|number}} is {{code|1}}:
'''Example 2:''' If {{code|number}} is {{code|1}}:
Line 36: Line 37:
[r: if(number >= 1, 20, "")]
[r: if(number >= 1, 20, "")]
</source>
</source>
'''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"}}:
'''Example 3:''' If {{code|variable}} is {{code|"Foobar"}}:
Line 42: Line 44:
[r: if(variable == "Text", 1, 0)]
[r: if(variable == "Text", 1, 0)]
</source>
</source>
'''Returns:''' {{code|0}}
:'''Returns:''' {{code|0}}
 


'''Example 4:''' If {{code|variable}} is {{code|20}}
'''Example 4:''' If {{code|variable}} is {{code|20}}
Line 48: Line 51:
[property = if(variable > 0 && variable < 20, 1, 0)]
[property = if(variable > 0 && variable < 20, 1, 0)]
</source>
</source>
'''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 its pretty handy to use this version - the function-version - since you can easily embed it in loops and expressions.  
'''Example 5:''' Usually its better to use the roll option version {{roll|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.
Lets say you want to check if one of a players tokens has Initiative you could do this like this.
Line 62: Line 64:
</source>
</source>


|how this code works in layman's terms=
This code asks a states something about a variable depending on what that variable is. It feeds back what you want it to, depending on the situation. A few simple examples using the d20 system of critical hits would be such:
'''Layman Example 1: If it is'''
This asks for the value of {{code|roll}}, and if that value turns out to be 20 it puts in the chat box {{code|Critical threat}}, and if not, it does nothing.
<source lang="mtmacro" line>
[h: roll=d20]
[r,if(roll==20): "Critical threat"]
</source>
'''Layman Example 2: if it is not'''
This asks for the value of {{code|roll}}, and if that value turns out to be anything but 20 it puts in the chat box {{code|no crit}}, and if it is 20, it does nothing.
<source lang="mtmacro" line>
[h: roll=d20]
[r,if(roll==20): "";"no crit"]
</source>
'''Layman Example 3: if it is, if it is not'''
This asks for the value of {{code|roll}}, and if that value turns out to be 20, then in the chat box it puts {{code|Critical Threat}}, and if it is not 20, it puts in the chat box {{code|no crit}}
<source lang="mtmacro" line>
[h: roll=d20]
[r,if(roll==20): "Critical threat";"no crit"]
</source>
|also=
|also=
[[Macros:Branching and Looping|Branching and Looping]], [[if (roll option)|if (roll option)]]
[[Macros:Branching and Looping|Branching and Looping]], [[if (roll option)|if (roll option)]]
}}
}}
[[Category:Miscellaneous Function]]
[[Category:Miscellaneous Function]]

Revision as of 08:55, 7 February 2012

if() Function

Introduced in version 1.3b38
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 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 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 executed if condition returns as true.
  • falseExpr - A section of code that is executed if condition does not return as true.

Examples

Example 1: If 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 to 0


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)]

See Also