From MapToolDoc
Jump to: navigation, search

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]")


  1. evalMacro(macroString)
  1. execMacro(macroString)


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


  1. [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.

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

Returns 15

  1. [h: TestVar3 = 10]
  2. [h: TestVar4 = 20]
  3. [r: evalMacro("[TestVar3+TestVar4]")]

Returns 30

  1. [h: TestVar3 = 15]
  2. [h: TestVar4 = 30]
  3. [h: evalMacro("[TestVar5 = TestVar3+TestVar4]")]
  4. [TestVar5]

Returns 45

  1. [h: TestVar6 = 20]
  2. [h: TestVar7 = 40]
  3. [r: execMacro("[TestVar6+TestVar7]")]

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

  1. [h: TestVar8 = 50]
  2. [h: TestVar9 = 100]
  3. [h: TestVar10 = 0]
  4. [h: execMacro("[TestVar10 = TestVar8+TestVar9]")]
  5. [TestVar10]

Returns 0

  1. [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