RPTools.net
http://forums.rptools.net/

Same code, very different results
http://forums.rptools.net/viewtopic.php?f=20&t=27174
Page 1 of 1

Author:  xavram [ Wed Apr 19, 2017 2:38 pm ]
Post subject:  Same code, very different results

This is in 1.4.0.5

I have this piece of code in 3 different macros.

Code:
[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?

Author:  xavram [ Wed Apr 19, 2017 2:40 pm ]
Post subject:  Re: Same code, very different results

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.

Author:  wolph42 [ Wed Apr 19, 2017 3:49 pm ]
Post subject:  Re: Same code, very different results

Did to check "apply to selected token"?

Author:  xavram [ Wed Apr 19, 2017 6:43 pm ]
Post subject:  Re: Same code, very different results

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.

Author:  xavram [ Wed Apr 19, 2017 7:10 pm ]
Post subject:  Re: Same code, very different results

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

Code:
[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.

Author:  aliasmask [ Thu Apr 20, 2017 12:44 am ]
Post subject:  Re: Same code, very different results

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.

Author:  wolph42 [ Thu Apr 20, 2017 2:26 am ]
Post subject:  Re: Same code, very different results

@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:
[hinitId getInitiativeToken()]
[
h, if(initId != ""), code: {
    [
token(initId):initValue getInitiative()]
};{
    [
initValue = -100]
}]

[
macro.return = initValue

Author:  aliasmask [ Thu Apr 20, 2017 3:27 am ]
Post subject:  Re: Same code, very different results

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

Author:  wolph42 [ Thu Apr 20, 2017 8:13 am ]
Post subject:  Re: Same code, very different results

interesting, I didn't know that and have never encountered the issue.

Page 1 of 1 All times are UTC - 6 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/