Remote Calling Lib Token macros

Thoughts, Help, Feature Requests, Bug Reports, Developing code for...

Moderators: dorpond, trevor, Azhrei

Forum rules
PLEASE don't post images of your entire desktop, attach entire campaign files when only a single file is needed, or generally act in some other anti-social behavior. :)
Post Reply
Jade
Giant
Posts: 149
Joined: Sun Jun 13, 2010 8:01 pm
Location: USA
Contact:

Remote Calling Lib Token macros

Post by Jade »

I'm trying to create a campaign macro that can call a Lib Token macro directly (for convenience).

For some reason, however, I get an error when I try to run the macro.

Contents:

Code: Select all

 [macro("Number Monsters (on)@Lib:Rules"): ""]
 [macro("Number Monsters (off)@Lib:Rules"): ""]
Error returned is:

Code: Select all

 Error in body of roll.
Statement options (if any): macro("Number Monsters (on)@Lib:Rules")
Statement Body : ""
Strange thing is that the macro works when I have Lib:Rules selected and the "Apply to selected tokens" option turned on (even though the macro being called doesn't have this turned on).

Any ideas what's going on?

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

Re: Remote Calling Lib Token macros

Post by aliasmask »

Sounds like you have an error in your code that requires a token impersonation. Perhaps at the top of your code you could put [H: switchToken(getSelected())] or something like that. Putting Apply to Selected on the campaign macro will do the same. Changing it for the lib macro doesn't do anything because it's not being clicked.

Jade
Giant
Posts: 149
Joined: Sun Jun 13, 2010 8:01 pm
Location: USA
Contact:

Re: Remote Calling Lib Token macros

Post by Jade »

But the called macro can be run on the Lib Token without errors, and running the campaign macro with the Lib Token selected still won't run (unless "apply to selected" is checked).

Also, I tried adding switchToken(). That still only works if "apply to selected" is checked, and then, for some reason, it won't run unless you have a token selected (though it does switch to the Lib Token when run).

Here is the called macro:

Code: Select all

[h, if(getMacroName()=="Number Monsters (on)"): onoff=0; onoff=1]

[h, if(onoff==0), code:
{[setMacroProps("Number Monsters (on)", "label=Number Monsters (off); color=gray")]};
{[setMacroProps("Number Monsters (off)", "label=Number Monsters (on); color=default")]}]

[h: monsters = getTokens(", ", '{"npc": 1; "unsetStates": ["Dead", "Auxiliary"]}')]
[h: nr = listCount(monsters)]


[h, for(i, 0, nr), code: {
  [switchToken(listGet(monsters,i))]
  [mid=strfind(getName(), '(?i)'+getProperty("mName")+' ?\\(?([0-9]*)\\)?', '')]
  [mnum=getGroup(mid, 1,1)]
  [if(mnum>9): mnum1=round((mnum-5)/10,0); mnum1=mnum]
  [if(mnum>9): mnum2=round(mnum-(mnum1*10),0); ""]
  [setState("MN"+mnum1, onoff)]
  [if(mnum>9): setState("MNx"+mnum2, onoff); ""]
}]

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

Re: Remote Calling Lib Token macros

Post by wolph42 »

it won't run cause its a far call (or whatever you call that), with which the currentToken does not 'stick'.

In other words, when the parser arrives at your your macro 'Number Monsters...()' there is no token 'available' to which the code should apply, so as soon as you use something like getProperty("Strength") it will break. You may have a token selected but that macro does not 'know' that. hence:

should start with something like:

Code: Select all

[switchToken(getSelected())]
like AM suggeted and you apparently ignored, so I'll repeat it for you.

Note that this will break when you have more than one tokens selected though,better would be

Code: Select all

[assert(getSelected()==1,"make sure only one token selected",0)]
[switchToken(getSelected())]
o and when you run the macro 'directly' with 'apply to selected tokens' obviously than there *will* be a currentToken. If you apply to selected token in the campaign menu BUT you use [macro:] to call a lib function than 'apply to selected token' will thus NOT stick. If you use a userdefinedfunction it will!

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

Re: Remote Calling Lib Token macros

Post by aliasmask »

After looking at your code, Wiki: setMacroProps() does need a current token to change. So, try putting this at the top of your code.

Code: Select all

[H: switchToken(getMacroLocation())]
In this case, Apply to Selected Token set on the calling macro will not help you, nor will switchToken(getSelected()).

Btw, I don't recommend doing what you're doing and changing the macro name on the lib token. It's better to just set a hidden variable. Also, since you're making the calls from the campaign window, it makes even less sense. Personally, I would just post to chat whether the button is on or off.

Also, I don't get why you're doing what you're doing. For example, from what I can tell, you get the token's number, like "creature 15" and save 15 in mnum. Then you set mnum1 to 2 and mnum2 to -5, then set states MN[#1], MNx[#2]. So, I assume you have 10 states for each MN[0-9] and MNx-[0-9]. It just seems like it's unnecessarily complicated. I'm just wondering what you're trying to accomplish, especially since you're calling the macro twice and all you're doing it turning it on then right back off.

Jade
Giant
Posts: 149
Joined: Sun Jun 13, 2010 8:01 pm
Location: USA
Contact:

Re: Remote Calling Lib Token macros

Post by Jade »

So, Kinks originally wrote this macro (for my framework), and I'm trying to "fix" a few things with it. So I can't speak for original intentions, but let me explain what it does:

The original macro (as found on the Lib Token, which does NOT have "apply to selected Tokens" checked), will, when depressed, add states numbering all Monsters on the board. Why is this useful? Well, all Monsters spawn in as Orc 1, Orc 2, Orc 3 (etc.) However, you can only see the name of the Monster in question by mousing over them. Sometimes, while in the middle of a spell or effect macro, you're required to select a Monster by name/image. If you're choosing between 8 different Orcs, it can be impossible to differentiate, and so Kinks created the number states toggle to provide a clear visual representation of which Orc is which, making it much easier to ensure that you select the appropriate Orc in the right context. The reason for two separate states is so that this will cover ALL Monsters numbered up to 99 without having to create 99 different states (we have it covered in 20). It is toggleable (on/off) because some people don't prefer the aesthetic of having huge numbers overlayed over all the Monsters. Kinks thought it would be nice if the button tracked to the toggle state, and so he did things the way he did with that.

I'm assuming Kinks wrote the Macro with the -5 to ensure it would round down (he probably wasn't aware that there's a function that does this already). I hadn't initially come into this with the intention of optimizing his code, but I may, particularly now that we've gotten into its guts anyway.

The reason I'm calling the macro twice is because the macro was written to be a toggle. Every time more Monsters are added, it needs (as written) to be turned off and then on to re-add the numbers. Yes, I could create two separate macros to handle these two separate functions (turning off and turning on numbering). However, that particular design choice doesn't seem relevant to my issue at hand.

My goal is simply to have a campaign macro that can re-add Monster numbers every time a new group of Monsters gets placed on the board. When I've got that working, I'm sure I could make it toggle without much difficulty.


Adding "switchToken()" specifying the Lib Token at the top of the called macro does fix the issue, though. I had tried switching the Token in the calling macro (specifying the Lib Token that the macro was always found on), but this, too, gave me an error. Not sure why it works when you switch the token in the called macro but not the calling macro. Do you know?

(For what it's worth, Wolph42, switching to selected doesn't work unless I add something that manually selects the Lib Token; it is not selected by default when running the calling macro.)

Ultimately, it was not initially clear to me that the problem was in the called macro, not in the calling macro, as the called macro runs fine by itself. As you (apparently) have been trying to point out (but I initially failed to understand), it was running correctly when called directly because then the Lib Token was automatically the "current Token", and this needed to be manually set within the called macro itself. What made it much more difficult for me to realize this is that apparently you can't just change the current Token in the calling macro. I'm still not clear on why that is.

Jade
Giant
Posts: 149
Joined: Sun Jun 13, 2010 8:01 pm
Location: USA
Contact:

Re: Remote Calling Lib Token macros

Post by Jade »

So, I've got a macro that copies some Tokens, and then I'm trying to get it to run this macro automatically to number the tokens immediately upon creation.

I'm noticing that when I stick in code to run this at the end of the former macro, it doesn't work. However, if I stick an input in the macro before the code to run this, it will work.

My guess as to why is that perhaps when the Macro normally runs, it does not create the tokens until the macro has finished running, but when you run an input on it, it creates the tokens and then finishes running.

Is there any way I can get it to create the tokens before it finishes running without the input dialog?

Post Reply

Return to “MapTool”