Page 1 of 1

Same code, very different results

Posted: Wed Apr 19, 2017 3:38 pm
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?

Re: Same code, very different results

Posted: Wed Apr 19, 2017 3:40 pm
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.

Re: Same code, very different results

Posted: Wed Apr 19, 2017 4:49 pm
by wolph42
Did to check "apply to selected token"?

Re: Same code, very different results

Posted: Wed Apr 19, 2017 7:43 pm
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.

Re: Same code, very different results

Posted: Wed Apr 19, 2017 8:10 pm
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.

Re: Same code, very different results

Posted: Thu Apr 20, 2017 1:44 am
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.

Re: Same code, very different results

Posted: Thu Apr 20, 2017 3:26 am
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] 

Re: Same code, very different results

Posted: Thu Apr 20, 2017 4:27 am
by aliasmask
MOTE ran in to this problem with that format where they had to force left to right evaluation.

Re: Same code, very different results

Posted: Thu Apr 20, 2017 9:13 am
by wolph42
interesting, I didn't know that and have never encountered the issue.