evalMacro: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(4 intermediate revisions by 3 users not shown)
Line 6: Line 6:
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.
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.  
If you are performing rolls in the macro that create tool tips or use {{code|[e: ]}} then you will have to use either {{code|{ <nowiki>}</nowiki>}} or {{code|[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).
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).


The advantage of this function over {{func|eval}} is that with {{func|eval}} you can only give a string as parameter that can be evaluated (e.g. "3+5"), while with {{func|evalMacro}} you can give anything as parameter but only the parts between [brackets] will be evaluated. (e.g. "Your resulting roll is [r:1d10]")
The advantage of this function over {{func|eval}} is that with {{func|eval}} you can only give a string as parameter that can be evaluated (e.g. {{code|"3+5"}}), while with {{func|evalMacro}} you can give anything as parameter but only the parts between {{code|[}}brackets{{code|]}} will be evaluated. (e.g. {{code|"Your resulting roll is [r:1d10]"}})


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


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




<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[r: evalMacro("[h: TestVar1 = 5][h: TestVar2 = 10][TestVar1+TestVar2]")]
[r: evalMacro("[h: TestVar1 = 5][h: TestVar2 = 10][TestVar1+TestVar2]")]
</source>
</syntaxhighlight>
Returns {{code|15}}
Returns {{code|15}}




<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[h: TestVar3 = 10]
[h: TestVar3 = 10]
[h: TestVar4 = 20]
[h: TestVar4 = 20]
[r: evalMacro("[TestVar3+TestVar4]")]
[r: evalMacro("[TestVar3+TestVar4]")]
</source>
</syntaxhighlight>
Returns {{code|30}}
Returns {{code|30}}




<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[h: TestVar3 = 15]
[h: TestVar3 = 15]
[h: TestVar4 = 30]
[h: TestVar4 = 30]
[h: evalMacro("[TestVar5 = TestVar3+TestVar4]")]
[h: evalMacro("[TestVar5 = TestVar3+TestVar4]")]
[TestVar5]
[TestVar5]
</source>
</syntaxhighlight>
Returns {{code|45}}
Returns {{code|45}}




<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[h: TestVar6 = 20]
[h: TestVar6 = 20]
[h: TestVar7 = 40]
[h: TestVar7 = 40]
[r: execMacro("[TestVar6+TestVar7]")]
[r: execMacro("[TestVar6+TestVar7]")]
</source>
</syntaxhighlight>
Prompts for the values of {{code|TestVar6}} and {{code|TestVar7}}, then it returns the sum of those two values.
Prompts for the values of {{code|TestVar6}} and {{code|TestVar7}}, then it returns the sum of those two values.




<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[h: TestVar8 = 50]
[h: TestVar8 = 50]
[h: TestVar9 = 100]
[h: TestVar9 = 100]
Line 66: Line 66:
[h: execMacro("[TestVar10 = TestVar8+TestVar9]")]
[h: execMacro("[TestVar10 = TestVar8+TestVar9]")]
[TestVar10]
[TestVar10]
</source>
</syntaxhighlight>
Returns {{code|0}}
Returns {{code|0}}




<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[r: evalMacro("[h:roll=1d20]You roll [r:roll] and you [r:if(roll<10, 'hit', 'miss')] your target.")]
[r: evalMacro("[h:roll=1d20]You roll [r:roll] and you [r:if(roll<10, 'hit', 'miss')] your target.")]
</source>
</syntaxhighlight>
Returns {{code|You roll 3 and you hit your target}}
Returns {{code|You roll 3 and you hit your target.}}





Latest revision as of 18:31, 14 March 2023

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).

The advantage of this function over eval() is that with eval() you can only give a string as parameter that can be evaluated (e.g. "3+5"), while with evalMacro() you can give anything as parameter but only the parts between [brackets] will be evaluated. (e.g. "Your resulting roll is [r:1d10]")

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


[r: evalMacro("[h:roll=1d20]You roll [r:roll] and you [r:if(roll<10, 'hit', 'miss')] your target.")]
Returns You roll 3 and you hit your target.

See Also