Trouble with IF statements

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
Nivek
Kobold
Posts: 5
Joined: Thu Oct 23, 2014 10:37 pm

Trouble with IF statements

Post by Nivek »

I'm trying to write a simple attack macro but am having trouble with IF statements. This is what I have so far:

Code: Select all

[h:status = input(
"TargetAC|0|Enter Target AC")]

swings his [r:Weapon] viciously! <br>
Attack Roll: <b>[r: 1d20]</b> vs. AC: <b>[r:TargetAC]</b>

[h: d20roll = 1d20]
[h:THAC0=getProperty("THAC0")]

[r:if(d20roll >= THAC0-TargetAC): "Hit!"]
Ideally, I'd like to replace the "Hit!" with a 1d8 damage roll.

I keep getting the following error:

Error in body of roll.       Statement options (if any): r       Statement Body : if(d20roll >= THAC0-TargetAC): "Hit!"


Thank you so much for your help.
Last edited by aliasmask on Fri Oct 24, 2014 12:04 am, edited 1 time in total.
Reason: added code block to text

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Trouble with IF statements

Post by aliasmask »

[if:] in the way you're trying use it is a roll option and needs a comma to separate it from the [r:] roll print option.

User avatar
Full Bleed
Demigod
Posts: 4736
Joined: Sun Feb 25, 2007 11:53 am
Location: FL

Re: Trouble with IF statements

Post by Full Bleed »

aliasmask wrote:[if:] in the way you're trying use it is a roll option and needs a comma to separate it from the [r:] roll print option.
Which means, this:

Code: Select all

[r:if(d20roll >= THAC0-TargetAC): "Hit!"]
Needs to look like this:

Code: Select all

[r, if(d20roll >= THAC0-TargetAC): "Hit!"]
You would use the colon if you were making code that looked something like this:

Code: Select all

[r: hitOrMiss = if(d20roll >= THAC0-TargetAC, "Hit!", "Miss!")]
Maptool is the Millennium Falcon of VTT's -- "She may not look like much, but she's got it where it counts."

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: Trouble with IF statements

Post by wolph42 »

The last one is explained here Wiki: if() and is thus different from [if:]

Another thing that took me ages to understand:

He core maptool script grammar is:

[ rollOption, rollOption, rollOption, etc : function() ]

So many options seperated by comma and one function seperated by one : (and the brackets around it)

There are exceptions obviously but this is the core.

Nivek
Kobold
Posts: 5
Joined: Thu Oct 23, 2014 10:37 pm

Re: Trouble with IF statements

Post by Nivek »

Thank you for the fix! The error message is gone. My current problem is that the math doesn't seem right. Here is my code:

[h:status = input(
"TargetAC|0|Enter Target AC")]

swings his [r:Weapon] viciously! <br>
Attack Roll: <b>[r: d20]</b> vs. AC: <b>[r:TargetAC]</b>

[h:d20roll=d20]
[h:THAC0=getProperty("THAC0")]

[r, if(d20roll >= THAC0-TargetAC): "HIT!"]

Here are some sample outputs:
Skjaaf has a THAC0 property of 19 and is attacking a foe with AC 10.

Skjaaf: swings his Axe +2 viciously! Attack Roll: 17 vs. AC: 10

17 >= 19-10 should be a HIT!

Skjaaf: swings his Axe +2 viciously! Attack Roll: 3 vs. AC: 10 HIT!

3 >= 19-10 should not return the true statement.

Thank you again for your help.

User avatar
Irrlicht
Dragon
Posts: 426
Joined: Mon Feb 09, 2009 10:53 am

Re: Trouble with IF statements

Post by Irrlicht »

If you set pieces of code exactly in the order you put them in the example, I'd suppose the problem is just a matter of visualization.

You place first this:
Attack Roll: <b>[r: d20]</b> vs. AC: <b>[r:TargetAC]</b>
And then this:
[h:d20roll=d20]

In the bold piece, you're getting a random d20 result, and then in the d20roll variable you store a different d20 result. This way, what you get to see in the output line, for example "Attack Roll: 15 vs. AC: 10" is different from what the IF below takes for the comparison, which may be any other d20 result.
(Basically, if you set "h:" in [h:d20roll=d20] to "r:", you should see that it has a different value than what's in the line above.)

Solution to this is simple... set the line to:
Attack Roll: <b>[r: d20roll=d20]</b> vs. AC: <b>[r:TargetAC]</b>
...and remove [h:d20roll=d20] below.
"There are many ways my Son, to find where the souls of Demons remain...
But it takes only one second of despair and of doubt until, at last, your Soul they will gain..."

Nivek
Kobold
Posts: 5
Joined: Thu Oct 23, 2014 10:37 pm

Re: Trouble with IF statements

Post by Nivek »

That worked great, thank you!

My token properties include "DamRoll", which I have listed as a die roll (1d8, 2d4, etc). I am trying to get my attack macro (below) to roll that damage on a successful hit. The IF statement isn't happy with rolling a die or calling up a property as a true value.

How can I get the macro below to call up the DamRoll property on a successful hit? Thanks.

[h:status = input(
"TargetAC|0|Enter Target AC")]

attacks with his [r:Weapon]! <br>
Attack Roll: <b>[r: d20roll=d20]</b> vs. AC: <b>[r:TargetAC]</b>

[h:THAC0=getProperty("THAC0")]
[h:DamRoll=getProperty("DamRoll")]

[r, if(d20roll >= THAC0-TargetAC): "and HITS!"] <br>
[r, if(d20roll >= THAC0-TargetAC): "Damage ="]

User avatar
Irrlicht
Dragon
Posts: 426
Joined: Mon Feb 09, 2009 10:53 am

Re: Trouble with IF statements

Post by Irrlicht »

I don't know which one is exactly your problem, but I'll point out these things:
1) You didn't add the damage value at the end, so it won't ever be displayed. The last piece should be changed to [r, if(d20roll >= THAC0-TargetAC): "Damage = " + DamRoll].
2) If the problem is with getProperty(), remember that in order to use it, you must do one (just one) of the following:
  • set Wiki: getProperty()'s id parameter, as specified in its wiki page;
  • switch the current token to the desired one by using Wiki: switchToken();
  • have the macro in your Campaign or Global panel, with the "Apply to Selected Tokens" box checked, and run it after selecting the desired token;
  • have the macro on each specific token's Selection panel (it acts automatically as if "Apply to Selected Tokens" is checked).
3) When a token is selected and you're using a macro that applies to selected tokens, you don't need to use getProperty() to take the values of its properties; if it has the THAC0 property, you just need to use THAC0 as if it was a variable you already set in that macro.
Also, if you change their values directly in the macro, for example writing something like [h: THAC0 = 10], you are effectively editing their token property; the selected token's THAC0 will become 10, if you write that. In fact, in the case of DamRoll, I assume you added it in token properties as "DamRoll:{1d8}", but if you call it in the macro by evaluating and redefining it with "[h: DamRoll =", it will be set to the random result determined in that moment. In fact, using [h: DamRoll = getProperty(DamRoll)] takes the DamRoll property, evaluates it, and let's say that the result is 5, and stores that 5 in the DamRoll variable that is used in the macro and also edits the token property by setting DamRoll to a fixed value of 5.

So, here's how I'd change your code (after putting it into a macro in the Selection panel or anyway one with "Apply to Selected Tokens" checked):

Code: Select all

[h:status = input(
"TargetAC|10|Enter Target AC")]

attacks with his [r:Weapon = "Sword"]! <br>
Attack Roll: <b>[r: d20roll=d20]</b> vs. AC: <b>[r:TargetAC]</b>

[r, if(d20roll >= THAC0-TargetAC): "and HITS!"]
[r, if(d20roll >= THAC0-TargetAC): "<br>Damage = " + DamRoll]
"There are many ways my Son, to find where the souls of Demons remain...
But it takes only one second of despair and of doubt until, at last, your Soul they will gain..."

Post Reply

Return to “Macros”