Same code, very different results

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
xavram
Dragon
Posts: 891
Joined: Tue Apr 20, 2010 8:22 pm

Same code, very different results

Post by xavram »

This is in 1.4.0.5

I have this piece of code in 3 different macros.

Code: Select all

[broadcast("1")]
[h, token(getInitiativeToken()), code: {
[h : tokenInit = getInitiative()]
}]
[broadcast(tokenInit)]
In two of them, I get the expected result in the chat window "1", then, "The token is not in the initiative list." Which I expect, because there are no tokens on the initiative board. This is what I expect and wanted to get back.

However, in the 3rd macro, I instead get this...

1
Error executing initiative function: there is no impersonated token.

So I'm pretty sure that the error is in that "code" section...but since I've copied it straight to each of the 3 macros, I'm quite confused about the inconsistent behavior.

All 3 macros are called from buttons on the Global panel, with only "Auto Execute" set on them. All 3 macros are on the same library token and again, only "Auto Execute" is checked off on the macros themselves.

Any ideas?

xavram
Dragon
Posts: 891
Joined: Tue Apr 20, 2010 8:22 pm

Re: Same code, very different results

Post by xavram »

Note that if I add the token to the initiative board, then it works fine and broadcasts the token's initiative value. But since these macros can be called when there are no tokens on the init board, it has to work in those situations as well.

Works fine for 2 of them, but not the 3rd.


xavram
Dragon
Posts: 891
Joined: Tue Apr 20, 2010 8:22 pm

Re: Same code, very different results

Post by xavram »

Nope, none of them. Later in the macros, it does a "getSelected" to figure out which tokens to apply the results to.

I'm going to see if I can work this a different way.

xavram
Dragon
Posts: 891
Joined: Tue Apr 20, 2010 8:22 pm

Re: Same code, very different results

Post by xavram »

I threw together a seperate macro that all three of these macros use instead.

Code: Select all

[h : initValue = -100]
[h : initId = getInitiativeToken()]
[h, if(initId != ""), code : {
	[h : switchToken(initId)]
	[h :  initValue = getInitiative()]
}]

[h : macro.return = initValue]
This seems to do the trick, if there's someone who has the initiative, it uses their initiative value, if there's no one on the init board, then it returns -100.

That fixes my issue.

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

Re: Same code, very different results

Post by aliasmask »

There's an important difference in the two macros. One macro uses two roll options side by side, ie token(tokenName), code: {} and the other separates them. MapTool may or may not go left to right when processing, but logically it could run the right side first with no access to the left, thus not having an impersonated token. Doing it the second way is the correct way to do this.

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

Re: Same code, very different results

Post by wolph42 »

@AM: I've never encountered an issue using any roll option in combination with the 'code' roll option. The 'left' side is ALWAYS evaluated before the 'code' part. Its another things when using e.g. the if() and count() roll options simultaneously.

The issue reported in this ine:
[h, token(getInitiativeToken()), code: {
is the simple fact that if there is no token in initiative you do:
[h, token(""), code: {
and that can generate an error. Using this code in different locations might result in the suppression of the error to the output (and the code not halting) while in other places it *does* throw an error and halts in that spot.

last code looks good, this is how I would do it:

Code: Select all

[h: initId = getInitiativeToken()]
[h, if(initId != ""), code: {
    [token(initId):initValue = getInitiative()]
};{
    [initValue = -100]
}]

[h : macro.return = initValue] 

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

Re: Same code, very different results

Post by aliasmask »

MOTE ran in to this problem with that format where they had to force left to right evaluation.


Post Reply

Return to “Macros”