Page 1 of 1

Problem with math.floor function in a macro

Posted: Tue Oct 31, 2017 6:52 pm
by TheIneffableCheese
I've added (what I think is) a very simple macro to a black pudding token for my Pathfinder campaign.

A black pudding has a Split ability that comes into play if it is struck by a piercing or slashing weapon. The rule is when struck by either a piercing or slashing weapon and the pudding has more than 10 HP, the pudding splits into two identical puddings each with half the hit points (rounded down because it is Pathfinder) of the original. I decided to automate this by adding a macro to the token that would do the dividing on the current token, then I can just copy and paste it to create the duplicate. Seems pretty straightforward.

FYI, the token has properties for HP and MaxHP. I also added printed lines (e.g., [r: "line 1"], [r: "line 2"], etc.) between each of the macro steps to see where the problem lies - it didn't even get through the first step, so I really am stuck. The other weird thing is the macro changes the value of the HP property to a null set before throwing the error.

Here's what I wrote:

Code: Select all

[h: HP=getProperty(HP)]
[h: assert(HP>10, "A Black Pudding with 10 or fewer HP cannot split")]
[h: NewHP=(HP/2)]
[h: NewHP=math.floor(NewHP)]
[h: setProperty(HP, NewHP)]
[h: setProperty(MaxHP, NewHP)]
I can't see any glaring errors, but when I run the macro the chat returns "illegal argument type java.lang.String, expecting java.math.BigDecimal"

That makes me think the program must be seeing the HP variable as a text string rather than a number, but I'm not sure how to make it realize that's not the case since the HP property is only ever a number.

Any help is greatly appreciated.

Re: Problem with math.floor function in a macro

Posted: Tue Oct 31, 2017 7:31 pm
by Full Bleed
TheIneffableCheese wrote:That makes me think the program must be seeing the HP variable as a text string rather than a number, but I'm not sure how to make it realize that's not the case since the HP property is only ever a number.
You can check if a value is a number by using Wiki: isNumber().

And you can try to change something into a number with Wiki: number().

EDIT:

And now that I've said the above I went and tested your code in my framework since I have a HP property... and wouldn't ya know... it thinks my HP property is a string, too (Wiki: isNumber() confirms). The error is not with math.floor though, it's with the assert which expects either a 0 or a 1 (the error is thrown with only the first two lines of your code).

Strange thing is I can't get Wiki: number() to force my HP value into a number. It throws the error:

Code: Select all

Argument number 1 "" to function "number" must be a number.
So... not sure what the problem is...

Re: Problem with math.floor function in a macro

Posted: Tue Oct 31, 2017 9:19 pm
by aliasmask
Is HP blank? Does HP have a default value? Is HP a global variable? If it's global, then the first line essentially says HP = HP. Also, for my asserts I usually put ,0 at the end to trim the message.

You could test these like this:

Code: Select all

[H: assert(! json.isEmpty(HP),"HP has a blank value",0)]
[H: assert(! isNumber(HP),"HP is not a value",0)]

Re: Problem with math.floor function in a macro

Posted: Tue Oct 31, 2017 9:39 pm
by Full Bleed
Can't speak for the op but in my case...
aliasmask wrote:Is HP blank?
Nope. Character I tested it on had a value of 31 in the HP property.
Does HP have a default value?
My campaign has a 10 default.
Is HP a global variable? If it's global, then the first line essentially says HP = HP.
Not sure what you mean with this.
You could test these like this:

Code: Select all

[H: assert(! json.isEmpty(HP),"HP has a blank value",0)]
[H: assert(! isNumber(HP),"HP is not a value",0)]
Running that I get the "not a value" result... which was expected because I already knew that Wiki: isNumber() does not see the value as a number (like I suspect is the OP's problem.) But I use all kinds of math on HP's in other macros... not the least of which on a damage :

Code: Select all

[h: HP = HP - damage]
So I'm not sure how that can be if it's a string and not a number.

Re: Problem with math.floor function in a macro

Posted: Wed Nov 01, 2017 12:09 am
by aliasmask
Derp!

What's wrong with this line?

Code: Select all

[h: HP=getProperty(HP)]
The same thing that is wrong with these lines:

Code: Select all

[h: setProperty(HP, NewHP)]
[h: setProperty(MaxHP, NewHP)]

Re: Problem with math.floor function in a macro

Posted: Wed Nov 01, 2017 3:47 am
by wolph42
missing quotes !

Re: Problem with math.floor function in a macro

Posted: Wed Nov 01, 2017 4:01 am
by Full Bleed
aliasmask wrote:Derp!

What's wrong with this line?
*Sigh*

It's always something silly. ;)

I just cut-n-pasted... and then when I tested isNumber I went down the same rabbit hole. I had a character sheet open so I never saw that the property was being jacked. Doh.

To the OP:

You need quotes around the Token Property:

Code: Select all

[h: HP = getProperty("HP")]
And, in general, I wouldn't use a temporary variable that is token property. If you run the macro on the token it will change the variable. To keep things clear, I usually only use capitalized variables when I am using a token property. Otherwise I only use lowercase variables.

Code: Select all

[h: hpCheck = getProperty("HP")]
[h: noSplit = if(hpCheck > 10, 1, 0)]
[h: assert(noSplit, "A Black Pudding with 10 or fewer HP cannot split")]
[h: newHP = floor(hpCheck/2)]
[h: setProperty ("HP", newHP)]
[h: setProperty ("MaxHP", newHP)]

Re: Problem with math.floor function in a macro

Posted: Wed Nov 01, 2017 6:27 pm
by TheIneffableCheese
wolph42 wrote:missing quotes !
D'oh!

I knew it had to be something stupid I had missed. :roll:

Thanks all. I just reworked the macro and it's working like a champ. :D

Also, thanks for the best practices advice on variable naming - I'll hold on to that for the future.