ifs, ands, ors (no buts)

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
Arcalane
Kobold
Posts: 9
Joined: Tue Sep 29, 2015 4:22 am

ifs, ands, ors (no buts)

Post by Arcalane »

Something quick - I'm being continually frustrated by an if check being recalcitrant about working with isPropertyEmpty().

Long story short, I'm running a game where there's multiple layers of health. I have a macro that applies damage, and I want to upgrade it so that it applies to shields first, or health if there are no shields remaining (but no overspill/overlap, this is intentional).

However, every time I try to use an if with isPropertyEmpty, it just gives me generic error messages.

Code: Select all

[if(isPropertyEmpty("SP") != 1 || SP > 0),CODE:{[h:SP = SP - damageTaken]};{[h:HP = HP - damageTaken]}]
So if SP is not null or is higher than 0, damage goes to SP. Otherwise it goes to HP.

Any ideas? I've tried setting a seperate value (e.g. hasShields) using isPropertyEmpty beforehand, but that isn't helping either. It works by itself, just not in the context of an if check. The whole thing works by itself, too, so long as the isPropertyEmpty check isn't included.

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

Re: ifs, ands, ors (no buts)

Post by wolph42 »

you give us little to go with,
- did you try to run isPropertyEmpty directly (and isolated) on the token? (and did it generate the same error?),
- what is the context of this macro (from where do you run it)?
- where's the rest of the code
- what error message?

EDIT: ah wait... what you do can't work: lets say that SP contains the value "" (or NULL) then in your code you effectively do
if("" > 0): etc.
OR
if(NULL > 0): etc.

which obviously generates an error.

I would suggest you initialize the SP value for all tokens and then reduce the statement to:

Code: Select all

[if(SP): SP = max(0, SP - damageTaken) ; HP = HP - damageTaken] 
unless you can have negative shieldpoints (which it certainly looks like) in which can you need to do

Code: Select all

[if(SP>0): SP = SP - damageTaken ; HP = HP - damageTaken] 

Arcalane
Kobold
Posts: 9
Joined: Tue Sep 29, 2015 4:22 am

Re: ifs, ands, ors (no buts)

Post by Arcalane »

wolph42 wrote:- did you try to run isPropertyEmpty directly (and isolated) on the token? (and did it generate the same error?),
Yes, and no, respectively. It works fine by itself. The other macro works fine without it. But if I try to tie the two together, it errors out.
wolph42 wrote:- what is the context of this macro (from where do you run it)?
Campaign tab, with Apply to Selected Tokens active.
wolph42 wrote:- what error message?

Code: Select all

   Error in body of roll.       
Statement options (if any): if(isPropertyEmpty("SP") != 1 && SP > 0),CODE       
Statement Body : {[h:SP = SP - damageTaken]};{[h:HP = HP - damageTaken]}
wolph42 wrote:EDIT: ah wait... what you do can't work: lets say that SP contains the value "" (or NULL) then in your code you effectively do
if("" > 0): etc.
OR
if(NULL > 0): etc.

which obviously generates an error.
This is why I'm trying to use isPropertyEmpty() instead of just calling the value itself; according to the wiki's documentation, isPropertyEmpty() should return 1 if the property is null, or 0 if the property is either a blank string or any other value. This is why it's been tripping me up.
wolph42 wrote:I would suggest you initialize the SP value for all tokens and then reduce the statement to:

Code: Select all

[if(SP): SP = max(0, SP - damageTaken) ; HP = HP - damageTaken] 
unless you can have negative shieldpoints (which it certainly looks like) in which can you need to do

Code: Select all

[if(SP>0): SP = SP - damageTaken ; HP = HP - damageTaken] 
This is basically the solution I'm running with at the moment, but it seems a little ugly as a workaround given the function I'm trying to use doesn't work as advertized.

Ed: I guess the point of the problem is that it seems to me that it shouldn't be too hard to check if a target has a shield value at all, and if it's above 0, then to apply damage to shields -- and if not, then to apply damage to health instead -- without having to do a workaround by having the default value for shields always be 0.

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: ifs, ands, ors (no buts)

Post by aliasmask »

I think the problem lies in you checking SP as a possible null and comparing it to a non null value. MACROSCRIPT doesn't check left to right and fail if one condition fails, it checks all the comparisons and if there is a type mismatch then you'll get your error. I would do it this way.

Code: Select all

[H, if(isPropertyEmpty(SP)): SP = 0]
[H, if(SP), code: { ... };{...}]
Basically, it assigns it a value of 0 if empty which will give you your false condition. This assumes the value of SP should be empty or a non-zero number.


I like to use Wiki: json.isEmpty() because it works with nulls, numbers, strings and jsons without throwing an error as well.

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

Re: ifs, ands, ors (no buts)

Post by wolph42 »

I think the problem lies in you checking SP as a possible null and comparing it to a non null value. MACROSCRIPT doesn't check left to right and fail if one condition fails, it checks all the comparisons and if there is a type mismatch then you'll get your error.
thats what I tried to point out (but apparently failed to do...)

I probly should have said:
then in your code you effectively do
if(isPropertyEmpty("SP") != 1 || "" > 0): etc.
OR
if(isPropertyEmpty("SP") != 1 || NULL > 0): etc.

Arcalane
Kobold
Posts: 9
Joined: Tue Sep 29, 2015 4:22 am

Re: ifs, ands, ors (no buts)

Post by Arcalane »

Right, I see now. I was expecting the second half to return a value that would be considered 0 if it was an empty string or null -- since it's done that in the past for LABEL fields in input();

Image

And yes, it still returns 0s for those if I use resetProperty on the fields (which are blank by default). In fact it even returns 0 if the value is a blank string like " " (two spaces).

Ed: and since I keep forgetting to mention it, I'm running MT 1.4.0.0 at the moment.

Post Reply

Return to “Macros”