code (roll option): Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
mNo edit summary
No edit summary
 
(3 intermediate revisions by 2 users not shown)
Line 7: Line 7:


====Usage====
====Usage====
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[CODE: { code_block }]
[CODE: { code_block }]
</source>
</syntaxhighlight>


The ''code_block'' is a collection of text and macro code, enclosed in a single {} pair. Everything within the {} is treated as a single block for the purposes of any looping or branching options.
The ''code_block'' is a collection of text and macro code, enclosed in a single {} pair. Everything within the {} is treated as a single block for the purposes of any looping or branching options.
Line 15: Line 15:
====Example====
====Example====


<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[h:num=5]
[h:num=5]
[WHILE(num > 0), CODE:
[WHILE(num > 0), CODE:
Line 23: Line 23:
   [num=num-1]
   [num=num-1]
}]
}]
</source>
</syntaxhighlight>


Outputs
Outputs
Line 40: Line 40:
To nest CODE:{} blocks, use a second CODE:{ } option, like so:
To nest CODE:{} blocks, use a second CODE:{ } option, like so:


<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[h:d20roll=1d20]
[h:d20roll=1d20]
[h:attackRoll=d20roll+AttackBonus]
[h:attackRoll=d20roll+AttackBonus]
Line 58: Line 58:
   The attack misses!
   The attack misses!
};]
};]
</source>
</syntaxhighlight>


MapTool can only handle two levels of nested code.
MapTool can only handle two levels of nested code.

Latest revision as of 23:59, 14 March 2023

 This article is a stub, you can help the RPTools Wiki project by contributing content to expand this article.

CODE

Introduced: Version 1.3.b46

The CODE option is used in conjunction with looping / branching options to execute multiple statements within a single "block" of a loop or branch, allowing the creation of more complex loops and branches.

Usage

[CODE: { code_block }]

The code_block is a collection of text and macro code, enclosed in a single {} pair. Everything within the {} is treated as a single block for the purposes of any looping or branching options.

Example

[h:num=5]
[WHILE(num > 0), CODE:
{
  This is iteration [r:num] <br>
  There are [r:num-1] iterations left<br>
  [num=num-1]
}]

Outputs

This is iteration 5 There are 4 iterations left
4, This is iteration 4 There are 3 iterations left
3, This is iteration 3 There are 2 iterations left
2, This is iteration 2 There are 1 iterations left
1, This is iteration 1 There are 0 iterations left
0

NOTE: the digit output at the beginning of each line is an artifact of the WHILE loop's evaluation of num - since this roll does not have the h option active, the result of that evaluation is displayed.

Nested CODE Blocks

To nest CODE:{} blocks, use a second CODE:{ } option, like so:

[h:d20roll=1d20]
[h:attackRoll=d20roll+AttackBonus]
[h,IF(attackRoll >= 16),CODE:
{
  [IF(d20roll == 20),CODE:
  {
    The attack is a critical hit!
    [h:damage=critDamage]
  };
  {
    The attack is a hit!
    [h:damage=regDamage]
  };]
};
{
  The attack misses!
};]

MapTool can only handle two levels of nested code.