evalMacro: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
m (The examples lacked their final ']' and thus will fail if copy/pasted into MapTools)
(Expanded the examples. The variable scope difference is large enough that I'm considering splitting execMacro into it's own article.)
Line 3: Line 3:
|trusted=true
|trusted=true
|version=1.3b49
|version=1.3b49
|description=Evaluates and "executes" the macro in a string and returns the result. The string contains the same type of macro commands that you would put in a token macro with the exception that it can not contain slash commands.
|description=
Evaluates and "executes" the macro in a string and returns the result. The string contains the same type of macro commands that you would put in a token macro with the exception that it can not contain slash commands.


If you are performing rolls in the macro that create tool tips or use [e: ] then you will have to use either { } or [r: ] to display the output otherwise you will get incorrect formatting. The evalMacro() function executes the macro in the same variable scope (i.e. the executed macro can read and alter variables from the current macro), where as execMacro() crates a new variable scope (i.e. the executed macro can neither read nor alter varaibles from the current macro).
If you are performing rolls in the macro that create tool tips or use '''[e: ]''' then you will have to use either '''{ }''' or '''[r: ]''' to display the output otherwise you will get incorrect formatting.  
 
The {{func|evalMacro}} function executes the macro in the same variable scope (i.e. the executed macro can read and alter variables from the current macro), where as {{func|execMacro}} creates a new variable scope (i.e. the executed macro can neither read nor alter varaibles from the current macro).


|usage=
|usage=
<source lang="mtmacro" line>
<source lang="mtmacro" line>
[h: result = evalMacro(macroString)]
evalMacro(macroString)
[h: result = execMacro(macroString)]
</source>
<source lang="mtmacro" line>
execMacro(macroString)
</source>
</source>
'''Parameter'''
{{param|macroString|The string containing the macro script that is evaluated/executed.}}


|examples=
|examples=
To set the notes of a token to the output generated by a macro.
<source lang="mtmacro" line>
<source lang="mtmacro" line>
[h: setNotes(evalMacro('[r,macro("CreateNotes@Lib:Notes"): ""]'))]
[h: setNotes(evalMacro('[r,macro("CreateNotes@Lib:Notes"): ""]'))]
</source>
</source>
Sets the Notes of a [[Token]] to the output of the {{code|CreateNotes}} macro located on the {{code|Lib:Notes}} [[Library Token]].


or


<source lang="mtmacro" line>
<source lang="mtmacro" line>
[h: setNotes(execMacro('[r,macro("CreateNotes@Lib:Notes"): ""]'))]
[r: evalMacro("[h: TestVar1 = 5][h: TestVar2 = 10][TestVar1+TestVar2]")]
</source>
</source>
Returns {{code|15}}
<source lang="mtmacro" line>
[h: TestVar3 = 10]
[h: TestVar4 = 20]
[r: evalMacro("[TestVar3+TestVar4]")]
</source>
Returns {{code|30}}
<source lang="mtmacro" line>
[h: TestVar3 = 15]
[h: TestVar4 = 30]
[h: evalMacro("[TestVar5 = TestVar3+TestVar4]")]
[TestVar5]
</source>
Returns {{code|45}}
<source lang="mtmacro" line>
[h: TestVar6 = 20]
[h: TestVar7 = 40]
[r: execMacro("[TestVar6+TestVar7]")]
</source>
Prompts for the values of {{code|TestVar6}} and {{code|TestVar7}}, then it returns the sum of those two values.
<source lang="mtmacro" line>
[h: TestVar8 = 50]
[h: TestVar9 = 100]
[h: TestVar10 = 0]
[h: execMacro("[TestVar10 = TestVar8+TestVar9]")]
[TestVar10]
</source>
Returns {{code|0}}
|also=
{{func|eval}}
}}
}}
[[Category:Miscellaneous Function]]
[[Category:Miscellaneous Function]]

Revision as of 00:38, 11 June 2009

evalMacro() Function

 Note: This function can only be used in a Trusted Macro

Introduced in version 1.3b49
Evaluates and "executes" the macro in a string and returns the result. The string contains the same type of macro commands that you would put in a token macro with the exception that it can not contain slash commands.

If you are performing rolls in the macro that create tool tips or use [e: ] then you will have to use either { } or [r: ] to display the output otherwise you will get incorrect formatting.

The evalMacro() function executes the macro in the same variable scope (i.e. the executed macro can read and alter variables from the current macro), where as execMacro() creates a new variable scope (i.e. the executed macro can neither read nor alter varaibles from the current macro).

Usage

evalMacro(macroString)
execMacro(macroString)

Parameter

  • macroString - The string containing the macro script that is evaluated/executed.

Examples

[h: setNotes(evalMacro('[r,macro("CreateNotes@Lib:Notes"): ""]'))]

Sets the Notes of a Token to the output of the CreateNotes macro located on the Lib:Notes Library Token.


[r: evalMacro("[h: TestVar1 = 5][h: TestVar2 = 10][TestVar1+TestVar2]")]

Returns 15


[h: TestVar3 = 10]
[h: TestVar4 = 20]
[r: evalMacro("[TestVar3+TestVar4]")]

Returns 30


[h: TestVar3 = 15]
[h: TestVar4 = 30]
[h: evalMacro("[TestVar5 = TestVar3+TestVar4]")]
[TestVar5]

Returns 45


[h: TestVar6 = 20]
[h: TestVar7 = 40]
[r: execMacro("[TestVar6+TestVar7]")]

Prompts for the values of TestVar6 and TestVar7, then it returns the sum of those two values.


[h: TestVar8 = 50]
[h: TestVar9 = 100]
[h: TestVar10 = 0]
[h: execMacro("[TestVar10 = TestVar8+TestVar9]")]
[TestVar10]
Returns 0

See Also