if (roll option): Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
(Redirect pending split of article.)
 
m (fixed example with extra )'s)
 
(20 intermediate revisions by 9 users not shown)
Line 1: Line 1:
#REDIRECT [[Macros:Branching and Looping]]
{{RollOption
[[Category:Roll Option]]
|name=if
|version=1.3b46
|description=
Branches the flow of the roll as determined by the condition.
 
|usage=
<syntaxhighlight lang="python">
[if(condition): true_body]
[if(condition): true_body; false_body]
</syntaxhighlight >
'''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|true_body|The roll that is executed if the {{code|condition}} evaluates to {{true}}. To use compound statements 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.}}
'''Operators'''<br>
Operators are used to compare two variables, strings, literal numbers, expressions, or function outputs within a {{code|condition}}.<br>
 
''Conditional Operators:''
* {{code|>}} - Greater than
* {{code|<}} - Less than
* {{code|>{{=}}}} - Greater than or equal to
* {{code|<{{=}}}} - Less than or equal to
* {{code|{{=}}{{=}}}} - Equal to
* {{code|!{{=}}}} or {{code|ne}} - Not equal
''Logical Operators:''
* {{code|&&}} - And
* {{code|{{!}}{{!}}}} - Or
''Boolean Operators:''
* {{code|true}}
* {{code|false}}
* {{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.
 
====Known Limitations====
* '''Number of () levels'''
The {{code|[if():]}} doesn't allow more than two levels of nested parenthesis {{code|()}}. Adding a third or more will produce a bad option parameters error.
<syntaxhighlight lang="python" line>
[R, if(((1))),code:{True};{False}]
</syntaxhighlight>
Produces
<syntaxhighlight lang="python">
Roll option "if": bad option parameters 1.       
Statement options (if any): r,if(((1))),code       
Statement Body : {True};{False}
Error trace : (new)@campaign
</syntaxhighlight>
This also applies to nesting function calls such as:
<syntaxhighlight lang="python" line>
[r,if(getName(getSelected()) == "Giant Rat"): val=1; val=0]
</syntaxhighlight>
This is okay:
<syntaxhighlight lang="python" line>
[r,if(getName() == "Giant Rat"): val=1; val=0]
</syntaxhighlight>
 
 
|example=
Sets the variable {{code|newVal}} to {{code|12*12}} if the variable {{code|val}} equals {{code|12}}.
<syntaxhighlight lang="python" line>
[h:val=12]
[h,if(val == 12): newVal=12*12]
New Value = [r: newVal]
</syntaxhighlight>
Returns {{code|New Value {{=}} 144}}
 
Example with logical operators:
<syntaxhighlight lang="python" line>
[h,if((val > 12 && val < 24) || val == 5): val=1 ; val=0]
</syntaxhighlight>
 
These examples perform the same function. If {{code|val}} is not a number, make {{code|val}} equal {{code|0}}. 
<syntaxhighlight lang="python" line>
[h, if (! isNumber(val)): val = 0]
</syntaxhighlight>
<syntaxhighlight lang="python" line>
[h, if (isNumber(val) == 0): val = 0]
</syntaxhighlight>
<syntaxhighlight lang="python" line>
[h, if (isNumber(val) == false): val = 0]
</syntaxhighlight>
 
Using {{code|code}} block:
<syntaxhighlight lang="python" line>
[if(condition),code:{
  [...]
  [...]
};{
  [...]
  [...]
}]
</syntaxhighlight>
 
 
|also=
{{func|if}},
{{func|isNumber}}, 
{{roll|code}},
[[Introduction to Macro Branching]]
}}
[[Category:Branching Roll Option]]
[[Category:Branching Roll Option]]

Latest revision as of 11:39, 17 November 2024

[if():] Roll Option

* Introduced in version 1.3b46

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 or false_body) is executed, if any. The condition/s can only contain one level of nested parenthesis.
  • true_body - The roll that is executed if the condition evaluates to true(1). To use compound statements in the true_body, you must use the [code():] roll option in conjunction with this roll option.
  • false_body - The roll that is executed if the condition evaluates to false(0). If no false_body is given, there is no output if the condition evaluates to false(0). To use complex rolls in the false_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
  • != or ne - 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.

Known Limitations

  • Number of () levels

The [if():] doesn't allow more than two levels of nested parenthesis (). Adding a third or more will produce a bad option parameters error.

[R, if(((1))),code:{True};{False}]

Produces

Roll option "if": bad option parameters 1.       
Statement options (if any): r,if(((1))),code       
Statement Body : {True};{False}
Error trace : (new)@campaign

This also applies to nesting function calls such as:

[r,if(getName(getSelected()) == "Giant Rat"): val=1; val=0]

This is okay:

[r,if(getName() == "Giant Rat"): val=1; val=0]

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]

Using code block:

[if(condition),code:{
  [...]
  [...]
};{
  [...]
  [...]
}]

See Also

if(), isNumber(), [code():], Introduction to Macro Branching