eval: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
m (Conversion script moved page Eval to eval: Converting page titles to lowercase)
m (Text replacement - "<source" to "<syntaxhighlight")
Line 8: Line 8:


|usage=
|usage=
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
eval(expr)
eval(expr)
</source>
</source>
Line 15: Line 15:


|examples=
|examples=
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[r: eval("1+1")]
[r: eval("1+1")]
</source>
</source>
Returns {{code|2}}
Returns {{code|2}}


<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[r: eval("3d6")]
[r: eval("3d6")]
</source>
</source>
Returns a random number from {{code|3}} to {{code|18}}.
Returns a random number from {{code|3}} to {{code|18}}.


<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[r: eval("TestVar = 2")]
[r: eval("TestVar = 2")]
</source>
</source>
Returns {{code|2}}
Returns {{code|2}}


<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[h: TestVar = 2][r: eval("TestVar/2")]
[h: TestVar = 2][r: eval("TestVar/2")]
</source>
</source>
Line 38: Line 38:
'''Tip'''
'''Tip'''
Since {{code|eval()}} only accepts strings as parameter and it happens quite often that you have either numbers or strings following code is a good method to make it work in any case.
Since {{code|eval()}} only accepts strings as parameter and it happens quite often that you have either numbers or strings following code is a good method to make it work in any case.
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[h: foo = eval(string(bar))]
[h: foo = eval(string(bar))]
</source>
</source>

Revision as of 18:01, 14 March 2023

eval() Function

Introduced in version pre-1.3
Evaluates an expression in a string and returns the result. The string contains the same type of expression that is usually located between macro brackets (e.g { } or [r: 2+2]). eval() is commonly used to evaluate dynamically built input() parameters, or dice expressions stored in token properties. For more complex evaluation functions, see evalMacro() and execMacro().

Usage

<syntaxhighlight lang="mtmacro" line> eval(expr) </source> Parameter

  • expr - A string containing the expression to be evaluated.

Examples

<syntaxhighlight lang="mtmacro" line>

[r: eval("1+1")] </source> Returns 2

<syntaxhighlight lang="mtmacro" line> [r: eval("3d6")] </source> Returns a random number from 3 to 18.

<syntaxhighlight lang="mtmacro" line> [r: eval("TestVar = 2")] </source> Returns 2

<syntaxhighlight lang="mtmacro" line> [h: TestVar = 2][r: eval("TestVar/2")] </source> Returns 1


Tip Since eval() only accepts strings as parameter and it happens quite often that you have either numbers or strings following code is a good method to make it work in any case. <syntaxhighlight lang="mtmacro" line> [h: foo = eval(string(bar))]

</source>

See Also