Edit 7/19/2010: Since I first wrote this post, I have learned more about MapTool macros, and you can find the improved information at this post. However, I’ve left the original post below for posterity – and as an example of how to learn how to improve your macro writing, rather than just giving the finished product.
Now that I’ve discussed the attack macros I’ve created in MapTool, I thought I’d mention one other macro that I’ve written for my current campaign, one that involves some additional functions of the MapTool language. This is a macro that rolls initiative.
As you may recall from my last post, I use the token properties in MapTool to store values for hit points, character level, ability scores, melee weapon die, etc. for my PCs. One of the properties that I store is Initiative – that is, the character’s initiative modifier. I also have the ability to do this for monsters. If I can write a macro that retrieves the initiative modifier from each token’s character sheet in MapTool, I can then roll up initiative.
I’ll start by mentioning that I like to roll initiative for each group of monsters only once (as I think is pretty standard in D&D 4e). So, if I have my party of three PCs facing down ten goblins, I roll initiative once for each PC and once for the goblins as a group – four separate rolls. If they’re facing 10 goblin warriors and 2 goblin dragonshields, I’ll roll separately for the warriors and dragonshields.
The macro that I’ve written looks at each token on the screen as I run it (whether visible to the players or covered by the fog of war – a topic for a future post), gets that token’s initiative modifier from its properties, and then rolls initiative. Now, if every goblin on the map has an initiative modifier, my macro will roll initiative separately for each one. My kludgy solution: Set the initiative modifier to the appropriate number for the FIRST monster of each group, and set the initiative modifier to the letter Z for all of the other monsters in the group. I can make Z the default value for initiative in the Campaign Properties if I like, to make this easier.
I’ll present the finished macro in all its glory, and then I’ll break it down for you.
<font size = 10><b>Initiative!</b></font><br>
[h: TokenList = getVisibleTokenNames()]
[FOREACH(name, TokenList, “”), CODE:
The first line is easy enough. It prints the word “Initiative!” in large letters (the <font size=10></font> tags) and in bold face (the <b></b> tags).
The next line is a little more interesting.
- The h: means that the result of this line will be hidden from the chat window (typical for background calculation commands in MapTool macros)
- TokenList = means that we’re creating a variable called “TokenList” and then assigning it a value
- getVisibleTokenNames() calls a MapTool function that creates a list of the names of all of the tokens on your screen. If you zoom in to just one token, it will get just the name of that token. If you zoom out and have 20 tokens on the screen, it will create a list of 20 names. This list of names is assigned to the variable called “TokenList”.
Next we have a FOREACH function. This says: For each name in the array called “TokenList”, I want you to execute whatever comes after the word CODE: in curly brackets. When you’re done executing the code for the first name and then for each subsequent name, I want you to not put any separator showing that you’re done (that’s what the “” means). If you don’t put the empty quotes there, MapTool will put a comma after the output of the CODE block when it’s done with each name.
Now we get into the first set of curly brackets (remember, I use indenting to help match up opening brackets with closing brackets to make the code easier to follow). The line that begins with IF means:
- Get the value of the property called “Intiative” for the token with the name that we’re currently working on from our FOREACH loop.
- If the Initiative value for this token is not equal to “Z” (the != means “is not equal to”), then do some code (which is going to be in the next set of curly brackets).
The next line starts with some curly brackets that enclose what we’re going to do if the current token’s initiative isn’t equal to “Z”. That is, it will print the name of the token in bold face (that’s the <b>[name]</b> bit). It will then roll a d20 and add the value of the Initiative property of the current token and print the result in the chat window. It will then print a line break (the <br>).
After that, you see the curly bracket close – which means that we’re done specifying what to do if the token’s Initiative value is not equal to “Z”. What comes next – the semicolon followed by the empty curly brackets – tells MapTool what to do in case the token’s Initiative value IS equal to “Z”. That is, nothing. If the token has a Z for initiative, then don’t roll initiative for it.
We then see a closing square bracket (matching the square bracket that opened right before the IF statement), then a closing curly bracket (matching the curly bracket that opened right before the IF statement), then a closing square bracket (matching the square bracket that opened right before the FOREACH statement. The result of this macro would look something like this:
Blue Slime: 3
Ochre Jelly: 9
Rat 1: 22
This is how I like to roll initiative in MapTool, though I know there are other ways. What is your preferred method?