switch (roll option): Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
No edit summary
m (Tweaking page layout and typography)
Line 17: Line 17:
{{param|case|A potential match for the {{code|expression}}, possesses a corresponding  {{code|body}} that is executed if a match is made.}}
{{param|case|A potential match for the {{code|expression}}, possesses a corresponding  {{code|body}} that is executed if a match is made.}}
{{param|default|If the {{code|expression}} finds no matches within the {{code|case}}s, then the {{code|defaultBody}} is executed.}}
{{param|default|If the {{code|expression}} finds no matches within the {{code|case}}s, then the {{code|defaultBody}} is executed.}}
'''Note''' that the 'case' statement is (ironically) the only case sensitive statement in maptool!, thus CASE or Case will NOT work!
'''Note''' that the {{code|case}} statement is (ironically) the only case sensitive statement in MapTool, thus {{code|CASE}} or {{code|Case}} will NOT work!


|examples=
|examples=
Line 27: Line 27:
case "daily": "You may only use this power once per day"]
case "daily": "You may only use this power once per day"]
</source>
</source>
Outputs {{code|You may use this power as much as you like}}
Outputs:
You may use this power as much as you like
 


<source lang="mtmacro" line>
<source lang="mtmacro" line>
Line 36: Line 38:
case "longsword": "a slashing weapon"]
case "longsword": "a slashing weapon"]
</source>
</source>
Outputs {{code|used for jabs, so is a puncturing weapon}}.  Notice that the first matching clause was the one that the {{roll|switch}} option found.
Outputs:
used for jabs, so is a puncturing weapon
Notice that the first matching clause was the one that the {{roll|switch}} option found.
 


When using the {{roll|code}} option with a {{code|switch}} option, each case body its own set of braces, like so:
When using the {{roll|code}} option with a {{code|switch}} option, each {{code|case}} body has its own set of braces, like so:
<source lang="mtmacro" line>
<source lang="mtmacro" line>
[h,switch(class),code:
[h,switch(class),code:

Revision as of 07:51, 13 October 2012

[switch():] Roll Option

* Introduced in version 1.3b46

Chooses among several options and executes code based on the expression.

Usage

[switch(expression):
   case case1: body1;
   case case2: body2;
   default: defaultBody
]

Parameters

  • expression - The regular expression that determines which case is executed. Since this is a regular expression, metacharacters such as * and () will need to have backslashes in front of them if you want to match them literally.
  • case - A potential match for the expression, possesses a corresponding body that is executed if a match is made.
  • default - If the expression finds no matches within the cases, then the defaultBody is executed.

Note that the case statement is (ironically) the only case sensitive statement in MapTool, thus CASE or Case will NOT work!

Examples

[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


[h:powerType=".*sword.*"]
[switch(powerType):
case "flail": "one-handed weapon; two-handed does Str*2 damage";
case "shortsword": "used for jabs, so is a puncturing weapon";
case "longsword": "a slashing weapon"]

Outputs:

used for jabs, so is a puncturing weapon

Notice that the first matching clause was the one that the [switch():] option found.


When using the [code():] option with a switch option, each case body has its own set of braces, like so:

[h,switch(class),code:
case "Warrior": {
  [Armor = 6]
  [beginningPowers = "Sword, Shield Bash, Bow, Shield, Torch"]
};
case "Rogue": {
  [Armor = 2]
  [beginningPowers = "Dagger, Hide, Backstab, Pick Lock, Torch"]
};
case "Wizard": {
  [Armor = 1]
  [beginningPowers = "Dagger, Staff, Light, Lightning Bolt, Fire Ball"]
};
case "Priest": {
  [Armor = 4]
  [beginningPowers = "Mace, Heal, Protect, Banish Undead, Torch"]
};
default: {
  [Armor = 0]
  [beginningPowers = "Fists, Feet"]
}]

See Also

if(), [if():], Introduction to Macro Branching