Macros:Branching and Looping: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
This page details the branching and looping structures in MapTool.
This page details the branching and looping structures in MapTool. With the exception of the Block If statement, these are all [[Macros:Roll:types | roll options]] and should follow the general form for roll options:
 
<source lang="mtmacro" line>
[option: body]
</source>
 
These may be combined with other roll options.


==Branching==
==Branching==
Line 20: Line 26:
===IF Option (introduced in Version 1.3.b46)===
===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 [[Macros:Roll:types | roll options]].  
This IF is a roll option (as mentioned above), but operates similarly to the block-style if.


;Usage
;Usage
Line 40: Line 46:
</source>
</source>


Displays ''New Value = 144''.
Outputs ''New Value = 144''.


===SWITCH Option===
===SWITCH Option===
Line 65: Line 71:
</source>
</source>


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


==Looping==
==Looping==
Line 88: Line 94:


Will iterate the ''Damage = Damage + 1d12'' operation 3 times.
Will iterate the ''Damage = Damage + 1d12'' operation 3 times.
===FOR Option===
Executes a statement for a number of iterations based on a start and end value.
====Usage====
<source lang="mtmacro" line>
[FOR(var, start, end): body]
[FOR(var, start, end, stepsize): body]
[FOR(var, start, end, stepsize, separator): body]
</source>
The ''var'' variable counts from ''start'' to ''end'' during the loop. The optional ''stepsize'' (default +1) is added to var at each iteration.
====Example====
<source lang="mtmacro" line>
[FOR(i,10,0,-2): "i is now " + i]
</source>
Counts down even numbers from 10 to 0.
===FOREACH Option===
Iterates over the contents of a string list in the format ''item1, item2, item3"
====Usage====
<source lang="mtmacro" line>
[FOREACH(var, list): body]
[FOREACH(var, list, output_separator): body]
[FOREACH(var, list, output_separator, list_separator): body]
</source>
====Example====
<source lang="mtmacro" line>
[h: enemyList="Orcs, Goblins, Ogres, Trolls"]
[FOREACH(enemy, enemyList, "<br>"): "You really hate" + enemy]
</source>
Outputs
You really hate Orcs
You really hate Goblins
You really hate Ogres
You really hate Trolls
===WHILE Option===
Repeatedly executes a statement until a condition becomes false.
====Usage====
<source lang="mtmacro" line>
[WHILE(condition): body]
[WHILE(condition, separator): body]
</source>
====Example====
<source lang="mtmacro" line>
[h:num=10]
[WHILE(num>=0): num = num-1]
</source>
Outputs ''9,8,7,6,5,4,3,2,1''

Revision as of 16:51, 21 December 2008

This page details the branching and looping structures in MapTool. With the exception of the Block If statement, these are all roll options and should follow the general form for roll options:

[option: body]

These may be combined with other roll options.

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)

This IF is a roll option (as mentioned above), but operates similarly to the block-style if.

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]

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

Outputs "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.

FOR Option

Executes a statement for a number of iterations based on a start and end value.

Usage

[FOR(var, start, end): body]
[FOR(var, start, end, stepsize): body]
[FOR(var, start, end, stepsize, separator): body]

The var variable counts from start to end during the loop. The optional stepsize (default +1) is added to var at each iteration.

Example

[FOR(i,10,0,-2): "i is now " + i]

Counts down even numbers from 10 to 0.

FOREACH Option

Iterates over the contents of a string list in the format item1, item2, item3"

Usage

[FOREACH(var, list): body]
[FOREACH(var, list, output_separator): body]
[FOREACH(var, list, output_separator, list_separator): body]

Example

[h: enemyList="Orcs, Goblins, Ogres, Trolls"]
[FOREACH(enemy, enemyList, "<br>"): "You really hate" + enemy]

Outputs

You really hate Orcs
You really hate Goblins
You really hate Ogres
You really hate Trolls

WHILE Option

Repeatedly executes a statement until a condition becomes false.

Usage

[WHILE(condition): body]
[WHILE(condition, separator): body]

Example

[h:num=10]
[WHILE(num>=0): num = num-1]

Outputs 9,8,7,6,5,4,3,2,1