eval: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
(Exanded article.)
Line 1: Line 1:
{{MacroFunction
{{MacroFunction
|name=eval
|name=eval
|description=Evaluates and "executes" the expression in a string and returns the result. The string contains the same type of expression that is between '''{}''' or '''[r: ]'''
|version=pre-1.3
|description=
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]'''). {{func|eval}} is commonly used to evaluate dynamically built {{func|input}} parameters, or dice expressions stored in token properties.
 
For more complex evaluation functions, see {{func|evalMacro}} and {{func|execMacro}}.


|usage=
|usage=
<source lang="mtmacro" line>
<source lang="mtmacro" line>
[h: result = eval(expr)]
eval(expr)
</source>
</source>
'''Parameter'''
{{param|expr|A string containing the expression to be evaluated.}}


|examples=
|examples=
Line 12: Line 18:
[r: eval("1+1")]
[r: eval("1+1")]
</source>
</source>
Returns
Returns {{code|2}}
    2


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


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


<source lang="mtmacro" line>
<source lang="mtmacro" line>
[h: blah = 2][r: eval("blah/2")]
[h: TestVar = 2][r: eval("TestVar/2")]
</source>
</source>
Returns
Returns {{code|1}}
    1
 
|also=
{{func|evalMacro}},
{{func|execMacro}}
 
}}
}}
[[Category:Miscellaneous Function]]
[[Category:Miscellaneous Function]]

Revision as of 00:11, 11 June 2009

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

eval(expr)

Parameter

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

Examples

[r: eval("1+1")]

Returns 2

[r: eval("3d6")]

Returns a random number from 3 to 18.

[r: eval("TestVar = 2")]

Returns 2

[h: TestVar = 2][r: eval("TestVar/2")]
Returns 1

See Also