Referencing a token property for an if() roll option

Thoughts, Help, Feature Requests, Bug Reports, Developing code for...

Moderators: Azhrei, dorpond, trevor

Forum rules
PLEASE don't post images of your entire desktop, attach entire campaign files when only a single file is needed, or generally act in some other anti-social behavior. :)
Post Reply
CyberRiot
Kobold
Posts: 8
Joined: Thu Apr 30, 2020 9:07 pm

Referencing a token property for an if() roll option

Post by CyberRiot »

So, I'm not a programmer, but I have had reasonable success with macros in MapTool, so far. But I'm having an issue referencing token properties for comparison against a die roll.

This is my original macro, and it works fine. But it requires you to type in your effective skill level.
[h: Skill = Effective_Skill_Level]
[Skill]
, Roll:
[T: Roll = 3d6]
,
[t,if(Roll <= Skill): output = "HIT!"; output = "MISS!"]
This returns the following results:
Character: « Skill = 12 » , Roll = 7, HIT!
, or
Character: « Skill = 10 » , Roll = 15, MISS!

Now, I'm trying to add more automation, and get it to pull the attack skill from the token properties. I have added the property "RAT" (an abbreviation for Ranged Attack Skill) to the tokens, and assigned a value of 13 to that property on the test token. I have ticked the "Apply to Selected Tokens" checkbox.

This is what I've done in the macro:
[h: Skill = "RAT"]
[Skill]
, Roll:
[T: Roll = 3d6]
,
[t,if(Roll <= Skill): output = "HIT!"; output = "MISS!"]
I also tried it like this:
[h: Skill = getProperty("RAT")]
[Skill]
, Roll:
[T: Roll = 3d6]
,
[t,if(Roll <= Skill): output = "HIT!"; output = "MISS!"]
Both of these return the following error message:
Invalid condition in IF(Roll = Skill) roll option.
Statement options (if any): t,if(Roll = Skill)
Statement Body : output = "HIT!"; output = "MISS!"
I assume this is because I am not correctly referencing the token property. And I'm afraid that I'm going to have to learn how to use JSON, but I'm trying to avoid it if possible. If this is the case, I'd appreciate some guidance on where to start reading. I've tried to read up on it a bit, but so far it just hasn't clicked for me.

Also, and this may be a whole different issue, but I am also going to want to calculate Skill as (RAT minus range), which means I also need to figure out how to select a target for the attack.

Thanks

User avatar
aliasmask
Deity
Posts: 8580
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Referencing a token property for an if() roll option

Post by aliasmask »

One of the best programming best practices is to follow the Get Stuff, Process Stuff, Output Stuff flow of logic. Basically, in a function/macro get everything you're going to need, process your values, then output/save those values. So, for your example I would do this.

Code: Select all

<!-- get -->
[H: skill = getProperty("RAT")]
[H: rollString = "3d6"]

<!-- process -->
[H: result = eval(rollString)]
[H: rolls = getRolled()]
[H: hitString = if(result <= skill,"HIT!","MISS!")]

<!-- generating an output template is still a part of process -->
[H: output = strformat('<span title="%{rolls}">%{rollString}: %{result} vs %{skill} - <b>%{hitString}</b></span>')]

<!-- output -->
[R: output]
The above assumes skill will always have a numeric value, but if doesn't, then there will be an error.

CyberRiot
Kobold
Posts: 8
Joined: Thu Apr 30, 2020 9:07 pm

Re: Referencing a token property for an if() roll option

Post by CyberRiot »

Thanks a bunch. This gives me a whole different approach to what I've been doing. I'm still very new to this.

Post Reply

Return to “MapTool”