code (roll option): Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
m (Conversion script moved page code (roll option) to Code (roll option) without leaving a redirect: Converting page title to first-letter uppercase)
m (Conversion script moved page Code (roll option) to code (roll option): Converting page titles to lowercase)
(No difference)

Revision as of 23:13, 9 February 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.