switch (roll option): Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(2 intermediate revisions by one other user not shown)
Line 6: Line 6:


|usage=
|usage=
<source lang="mtmacro" line>
<syntaxhighlight  lang="mtmacro" line>
[switch(expression):
[switch(expression):
   case case1: body1;
   case case1: body1;
Line 12: Line 12:
   default: defaultBody
   default: defaultBody
]
]
</source>
</syntaxhighlight>
'''Parameters'''
'''Parameters'''
{{param|expression|The ''regular expression'' that determines which case is executed. Since this is a ''regular expression'', metacharacters such as {{code|*}} and {{code|()}} will need to have backslashes in front of them if you want to match them literally.}}
{{param|expression|The ''regular expression'' that determines which case is executed. Since this is a ''regular expression'', metacharacters such as {{code|*}} and {{code|()}} will need to have backslashes in front of them if you want to match them literally.}}
Line 23: Line 23:


|examples=
|examples=
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[h:powerType="at-will"]
[h:powerType="at-will"]
[switch(powerType):
[switch(powerType):
Line 29: Line 29:
case "encounter": "You may only use this power once per encounter";
case "encounter": "You may only use this power once per encounter";
case "daily": "You may only use this power once per day"]
case "daily": "You may only use this power once per day"]
</source>
</syntaxhighlight>
Outputs:
Outputs:
  You may use this power as much as you like
  You may use this power as much as you like




<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[h:powerType=".*sword.*"]
[h:powerType=".*sword.*"]
[switch(powerType):
[switch(powerType):
Line 40: Line 40:
case "shortsword": "used for jabs, so is a puncturing weapon";
case "shortsword": "used for jabs, so is a puncturing weapon";
case "longsword": "a slashing weapon"]
case "longsword": "a slashing weapon"]
</source>
</syntaxhighlight>
Outputs:
Outputs:
  used for jabs, so is a puncturing weapon
  used for jabs, so is a puncturing weapon
Line 46: Line 46:


another example
another example
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[abort(input("number|0|enter a number"))]
[abort(input("number|0|enter a number"))]
[switch(number):
[switch(number):
Line 54: Line 54:
default: "you entered a number not equal to 1 or 2"
default: "you entered a number not equal to 1 or 2"
]
]
</source>
</syntaxhighlight>


When using the {{roll|code}} option with a {{code|switch}} option, each {{code|case}} body has 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>
<syntaxhighlight lang="mtmacro" line>
[h,switch(class),code:
[h,switch(class),code:
case "Warrior": {
case "Warrior": {
Line 79: Line 79:
   [beginningPowers = "Fists, Feet"]
   [beginningPowers = "Fists, Feet"]
}]
}]
</source>
</syntaxhighlight>


|also=
|also=

Latest revision as of 17:30, 14 March 2023

[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.

Notes

  • the case and default statements are the only case-sensitive statements in MapTool, thus CASE or Default will NOT work!
  • strings in the case MUST be surrounded by "double quotes" as 'single quotes' will generate an error.
  • do not put a ; after your last case.

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.

another example

[abort(input("number|0|enter a number"))]
[switch(number):
	case 0: "you did not enter a number";
	case 1: "you entered 1 as a number";
	case 2: "you entered 2 as a number";
	default: "you entered a number not equal to 1 or 2"
]

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