if (roll option): Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 6: Line 6:


|usage=
|usage=
<source lang="mtmacro">
<source lang="python">
[if(condition): true_body]
[if(condition): true_body]
[if(condition): true_body; false_body]
[if(condition): true_body; false_body]
Line 33: Line 33:
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.


====-- Known Problems====
====Known Limitations====
* '''Number of () levels'''
* '''Number of () levels'''
The {{code|[if():]}} doesn't allow more than one level of {{code|()}}. So,
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.


<source lang="mtmacro" line>
<syntaxhighlight lang="python" line>
[R, if(((1))): "true";"false"]
[R, if(((1))),code:{True};{False}]
</source>
</syntaxhighlight>
 
Produces
will give an error.<br>
<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>


* '''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>


|example=
|example=
Sets the variable {{code|newVal}} to {{code|12*12}} if the variable {{code|val}} equals {{code|12}}.
Sets the variable {{code|newVal}} to {{code|12*12}} if the variable {{code|val}} equals {{code|12}}.
<source lang="mtmacro" line>
<syntaxhighlight lang="python" line>
[h:val=12]
[h:val=12]
[h,if(val == 12): newVal=12*12]
[h,if(val == 12): newVal=12*12]
New Value = [r: newVal]
New Value = [r: newVal]
</source>
</syntaxhighlight>
Returns {{code|New Value {{=}} 144}}
Returns {{code|New Value {{=}} 144}}


Example with logical operators:
Example with logical operators:
<source lang="mtmacro" line>
<syntaxhighlight lang="python" line>
[h,if((val > 12 && val < 24) || val == 5): val=1 ; val=0]
[h,if((val > 12 && val < 24) || val == 5): val=1 ; val=0]
</source>
</syntaxhighlight>


These examples perform the same function. If {{code|val}} is not a number, make {{code|val}} equal {{code|0}}.   
These examples perform the same function. If {{code|val}} is not a number, make {{code|val}} equal {{code|0}}.   
<source lang="mtmacro" line>
<syntaxhighlight lang="python" line>
[h, if (! isNumber(val)): val = 0)]
[h, if (! isNumber(val)): val = 0)]
</source><source lang="mtmacro" line>
</syntaxhighlight>
<syntaxhighlight lang="python" line>
[h, if (isNumber(val) == 0): val = 0)]
[h, if (isNumber(val) == 0): val = 0)]
</source><source lang="mtmacro" line>
</syntaxhighlight>
<syntaxhighlight lang="python" line>
[h, if (isNumber(val) == false): val = 0)]
[h, if (isNumber(val) == false): val = 0)]
</source>
</syntaxhighlight>


Using {{code|code}} block:
Using {{code|code}} block:
<source lang="mtmacro" line>
<syntaxhighlight lang="python" line>
[if(condition),code:{
[if(condition),code:{
   [...]
   [...]
Line 82: Line 83:
   [...]
   [...]
}]
}]
</source>
</syntaxhighlight>


The following will generate an error:
The following will generate an error:
<source lang="mtmacro" line>
<syntaxhighlight lang="python" line>
[h,if(getName(getSelected()) == "Giant Rat"): val=1]  --- ERROR, too many parenthesis on condition!
[h,if(getName(getSelected()) == "Giant Rat"): val=1]  --- ERROR, too many parenthesis on condition!
[h,if(getName() == "Giant Rat")): val=1] ---  This is OK.
[h,if(getName() == "Giant Rat")): val=1] ---  This is OK.
</source>
</syntaxhighlight>


|also=
|also=

Revision as of 16:35, 18 June 2021

[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

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:{
  [...]
  [...]
};{
  [...]
  [...]
}]

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