Macros:Branching and Looping

From RPTools Wiki
Revision as of 16:28, 21 December 2008 by Cclouser (talk | contribs)
Jump to navigation Jump to search

This page details the branching and looping structures in MapTool.

Branching

Block IF()

Usage
[h: if(condition, true_value, false_value)]
Example
[h: val=12]
[h: if(val > 10, "Value is greater", "Value is less")]

Returns Value is greater.

IF Option (introduced in Version 1.3.b46)

The second IF() structure is a roll option, placed inside square brackets prior to the colon, as with other roll options.

Usage
[IF(condition): true_body; false_body]

or

[IF(condition): true_body]

Either the true_body or false_body will be used, depending on the value of condition. If the false_body is not given but the condition is false, then there is no output.

Example
[h:val=12]
[IF(val == 12): newVal=12*12]
New Value = [r:newVal]

Displays New Value = 144.

SWITCH Option

SWITCH chooses among several options and executes code based on the switch expression.

Usage

[SWITCH(expression):
case case1: body1;
case case2: body2;
default: default_body]

Example

[h:powerType="at-will"]
[SWITCH(powerType):
case "at-will": "You may use this power as much as you like";
case "encounter": "You may only use this power once per encounter";
case "daily": "You may only use this power once per day"]

Displays "You may use this power as much as you like"

Looping

COUNT Option

The COUNT option executes a statement a specified number of times, storing the number of the current iteration in a variable called roll.count.

Usage

[COUNT(num): body]
[COUNT(num, separator): body]

The roll.count variable will take on values from 0 to (number of loops - 1). The optional separator (default ", ") is printed between each iteration.

Example

[h:numHits=3]
[COUNT(numHits): Damage = Damage + 1d12]

Will iterate the Damage = Damage + 1d12 operation 3 times.