eval: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
(Exanded article.)
No edit summary
Line 35: Line 35:
Returns {{code|1}}
Returns {{code|1}}


'''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.
<source lang="mtmacro" line>
[h: foo = eval(string(bar))]
</source>
|also=
|also=
{{func|evalMacro}},  
{{func|evalMacro}},  

Revision as of 10:07, 18 June 2010

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


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.

[h: foo = eval(string(bar))]

See Also