Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Selecting a token by name via Macro or MOD

January 06 (2 years ago)

New to mods, but I haven't seen an answer for this yet.  Can you preselect a token by static name via macro or MOD before executing additional macro lines which requires a token to be selected?

The token name is indeed static, not a dynamic name, not reliant on who initiates the macro.


January 06 (2 years ago)
keithcurtis
Forum Champion
Marketplace Creator
API Scripter

Hi JP!

There are generally three ways to indicate a token in a macro:

1. By Name

@{Gilgamesh|strength)

2. By whether it is the selected token

@{selected|strength}

3. By target

@{target|strength}

Target can select multiple targets with some extra verbiage, though what you can do with those targets might have limitations.


As to what you can do with that, well that can get complicated. It might help us give a more meaningful answer if you described why you were trying to do this, not how or if it can be done.

January 06 (2 years ago)

Thank you for the reply.

I've created a multi sided Roll Table Token and I've found a mod to randomly change the side.  This will reside on the table top to display for all players as flavor for critical hits and fumbles.  The sides represent different consequences of the roll.  I've also setup a listener for said critical and fumble rolls which I would like to trigger the random image change mod with. 

The image changing mod requires the multi sided token to be selected to function and I'm not versed well enough in javascript, nor have I found examples of how to stipulate a specific token name in a mod to modify the code directly.

So my question pertains to this.  If I can have the specific token selected via macro then I can immediately call the change image macro.  This can then be triggered automatically via the listener on crits and fumbles regardless of who's rolling. 

I have the image changer working on a button click if the token is selected and I have the listener working separately as well.  Trying to link all this together results in an error because the multi sided token is not selected in the order of operations.  

So, if I'm able to select a specific token via macro or mod, I imagine it would solve this issue.  If by macro, I'll include it in the macro run by the listener, if by mod, I'll prefix the change image code with the function to do so or call it as a separate mode first via the listener.

I hope I was able to explain this clear enough as I'm just learning the terminology. 

January 06 (2 years ago)
keithcurtis
Forum Champion
Marketplace Creator
API Scripter

That helps a lot, actually. Can you name or post links to the two mods you are using? For this usage, I would likely recommend Customizable Roll Listener and Token-Mod, both available from One Click install. It would probably be pretty easy to set up. For instance, Token-mod can accept a macro with a pre-defined token. But if you can list what you are using, we might be able to find a way to get it to do what you want it to do.

January 06 (2 years ago)
timmaugh
Forum Champion
API Scripter

SelectManager will do this in the command line of your existing macro.

{& select The token}

Or

{& select -M1234567890abcdef}


January 06 (2 years ago)

Thanks for the replies. I'm using Token Roller from Github, this is the one requiring a token to be selected.  https://gist.github.com/Tsolval/0166346e7528d16839b026fcff264a42

The other is just a listener.

I'll also take a look at the SelectManager.

January 06 (2 years ago)
timmaugh
Forum Champion
API Scripter

Just to be clear, if you just need the token to be selected for the purposes of Token Roller, SelectManager will work. Just add one of the formations from my last post to your command line intended for Token Roller.

If you need to extract data from the selected token or the associated character while you're still in the command line (ie, @{selected|hp} ), then you will want to get Fetch and ZeroFrame, too. This is due to timing issues, requiring SelectManager to run *after* Roll20 parsers have at the message, meaning that you can't use Roll20 constructions that are designed to get info from the token selected... the correct token isn't selected until SelectManager runs.

Then you'd use a Fetch construction: @(selected.hp)

More info here.

January 06 (2 years ago)

Hello Tim,

Please forgive my ignorance here, but I am unable to get the select manager to function as I think it needs to.  I have installed Token Mod, Messenger, SelectManager. 

If I have an unrelated token select via mouse click, blue box around it etc, what is the exact syntax I need to run in order to change the selection to my desired token?  I've tried the below to no avail.

!{@ select TokenName}

!(@ select -TokenName}

!token-mod --ids {& select TokenName}

January 06 (2 years ago)

Edited January 06 (2 years ago)
timmaugh
Forum Champion
API Scripter

No worries, JP... so, {&select...} doesn't change the selection on the VTT (where you'd imagine running it on its own), nor does it leave a remnant behind in the command line (like your last example where you have it after the --ids argument of your token-mod line).

===== JavaScript Explanation (feel free to skip) =====
If the command line you issue from chat begins with a bang (!), the Roll20 parser examines the message for stuff like roll queries, inline rolls, sheet retrieval calls, etc., then it creates a javascript object that gets handed off to the stack of scripts you have installed. If you had a token selected at the time you issued the command, the created object has a selected property, with the token information for all the tokens you had selected. The created object passes through each script you have installed, one at a time, in the order you installed the scripts... The important part being that they're passing the same object to the next script, and then to the next. SelectManager, like all of metascripts, actually does some trickeration/shin-kicking to get to the start of the script stack so it touches the message first. That means that it gets a chance to modify (or add) the selected property, adding the appropriate tokens to the object that it then passes on to your standard scripts (like Token-Mod). So by the time Token-Mod gets a chance at the message, it only knows that there are tokens in the selected property. It doesn't know that the list that it sees there is potentially different from the actual state of the VTT.

Without knowing what your command is, imagine a token representing a timer that you were moving from left to right to give a visual indication of an approaching deadline. When the token his some boundary to the right, the players have run out of time. The following Token-Mod command will move whatever token you have selected one square to the right:

!token-mod --move =90|1u

But that will work with a token you already have selected. If you don't want to have to select the token, you can use the --ids argument and provide a token ID to Token-Mod, or you can use SelectManager to "virtually" select the token by name (imagine you've named the token HourGlass):

!token-mod --move =90|1u {&select HourGlass}

In this case, SelectManager is duplicating some Token-Mod functionality since you can provide the token id to TokenMod so you don't have to select it every time. However, if you have a timer on every map, they will each have a separate id. To save yourself from having to provide a new id to pre-constructed macros (one-macro-per-page), you can name all of the timers "HourGlass", and your one macro will select the appropriate token no matter what map you're on.

(Also, some scripts don't have the built-in flexibility that Token-Mod has to even give you the opportunity to provide an alternate token to the one you have selected.)

January 06 (2 years ago)

Edited January 06 (2 years ago)

This was very informative, thank you. I believe I have a better understanding of how these things are working.   The Token Roller appears to require the item to be selected on the VTT or at least, it is pulling information from the token, namely, the various sides of the multisided token.

Based on the info you've provided, I feel that I would need to modify the Token Roller to select the token then use the fetch/zeroframe to pull information from it to get it to work for the rest of that mod.  Changing the selection on the VTT isn't done through the SelectionManager which is what I was hoping would be a short cut to not having to modify the Token Roller.

Cluing in a little further however, all I actually need is the below since I know the number of sides and I can ditch the Token Roller all together.

!token-mod --set currentside|[[1d6]] {& select TokenName}

The above works on a macro button press regardless of the token selected on the VTT which I'm very happy with.

Just having a little trouble getting the listener to run it.  Trying to add it to the Listener directly displays it like this !token-mod --set currentside|$[[0]]  or if I reference a macro that contains the code in the listener, it comes up with the same.

I've tried these for the Customizable Roll Listener:

!crl \\edit,name=Listener2,text=##,template=atk atkdmg,roll=fumble \\#Crit-Fail-Melee

!crl \\edit,name=Listener2,text=##,template=atk atkdmg,roll=fumble \\!token-mod --set currentside|[[1d6]] {& select CritDice}

I feel I'm missing something simple here, but am oh so close.

January 06 (2 years ago)
timmaugh
Forum Champion
API Scripter


JP G. said:

The Token Roller appears to require the item to be selected on the VTT or at least, it is pulling information from the token, namely, the various sides of the multisided token.

Based on the info you've provided, I feel that I would need to modify the Token Roller to select the token then use the fetch/zeroframe to pull information from it to get it to work for the rest of that mod.  Changing the selection on the VTT isn't done through the SelectionManager which is what I was hoping would be a short cut to not having to modify the Token Roller.

Actually, *no* script knows what is actually selected on the VTT.

Scripts responding to chat events only know what is provided them in the message object I described. When you send a bangsy message, part of what Roll20 does on the backend is to collect certain data from the VTT (like the tokens you have selected). It populates the selected property of the message object with that data, which is what the scripts read. Using that data, they can identify the token from which you want information, or the character from which you want information (who is associated with that token).

SelectManager just overwrites that property (or adds to what is already there it if you use {&inject ... } rather than {&select ... } ).

As for why the Custom Roll Listener isn't working... I don't use it, myself, but I can see one thing that is going wrong. When you use this formation:

!crl \\edit,name=Listener2,text=##,template=atk atkdmg,roll=fumble \\!token-mod --set currentside|[[1d6]] {& select CritDice}

What happens is that this command is intended for CRL to establish 1) the thing to listen for, and 2) what command to issue when the listened-for thing is heard. The original message object passes into the script stack, and SelectManager sees the {& select ... } construction. It figures that you want to immediately select the CritDice token, so it modifies the message object and REMOVES this syntax from the command line. When CRL sees the command line, it reads the text it needs to dispatch as:

!token-mod --set currentside|[[1d6]]

Without the select construction.

There are a few other scripts that can issue outbound commands to other scripts, and when the need has come up I've worked with the script authors to provide an "escape" character to make sure that the meta-construction isn't eaten in the original message. For example, using ^ as a deferral character, the select construction would be:

{^& select ... }

The dispatching script (in this case, CRL), would remove the escape character before dispatching the message, thus exposing the meta-constructions for the dispatched message:

{& select ... }

However, we haven't gotten CRL modified to do that, too.

However-however, you don't need to modify CRL for this one use-case... because all you're doing is designating the token you want to use (for the Token-Mod command). You can do that with Token-Mod syntax:

!token-mod --set currentside|[[1d6]] --ids -M1234567890abcdef

Where you'd obviously include the actual id of the CritDice token. (Again, this is the solution for a single token of known id... so it will only work on that one token.)

January 06 (2 years ago)

Thanks Tim, this has been extremally educational. It looks like was trying to go the long way around with my original solution, what you've provided is simple and straight forward.  I've got this to work outside the listener via macro click without issue as you've suggested. 

It's still not working from the listener however, even if I specify a side to switch to instead of rolling a number. Apparently, rolling is not executed inside the listener. 

I sincerely appreciate you taking the time to walk me through the logic and terms, I've certainly learned a lot.

Keith, if you've worked with the listener, have you gotten this to work?

I feel like I've wasted a lot of your time on this....

January 06 (2 years ago)

Edited January 06 (2 years ago)
timmaugh
Forum Champion
API Scripter

Oh, one other thing...

One script issuing a command for another (CRL sending the TM command), creates a message that doesn't actually come from a player (it comes from the Script Moderator, itself -- like a virtual player at your table).

You can try adding the --api-as argument to your token-mod line:

!token-mod --set currentside|[[1d6]] --ids -M1234567890abcdef --api-as <playerid>

...where you'd supply the playerid of the person Token-Mod should "use" as the source of the command (it will check for permissions). The player id is unique for each player in each game (so different for you between 2 separate games you are in). There are ways to get this, but I tend to just use Fetch and get it (in your case) when the CRL command is received:

!token-mod --set currentside|[[1d6]] --ids -M1234567890abcdef --api-as @(player.timmaugh.player_id)

EDIT: This requires the Fetch metascript.

January 06 (2 years ago)
keithcurtis
Forum Champion
Marketplace Creator
API Scripter

Sorry, I dropped out since it looked like you and timmaugh were galloping away on this. If the issue is that the die roll is resolving at the wrong time, then instead of trying to roll a die roll in the referred command, try using the random side syntax of token-mod:

!token-mod --set currentside|*
January 06 (2 years ago)

Thank you Keith, 

Token-mod command still isn't firing I'm afraid.

I'm using:

!crl \\edit,name=Listener1,text=##,tempate=atk atkdmg,roll=critical \\!token-mod --set currentside|* --ids -NL2sdlukxO4tH3DO_Pg \\CRITICAL HIT

The critical hit text comes up, but the token isn't switching sides.
Running a macro/command line outside of the listener does switch the sides. So it is working on its own, just not in the listener.

If this is something that isn't meant to work, I'm OK with that.

January 06 (2 years ago)

Taking the token out of the equation, I guess you can't use the Roll Listener to auto Roll Table results to chat either?

January 06 (2 years ago)
timmaugh
Forum Champion
API Scripter

Did you try the --api-as argument mentioned above? Having the token-mod command work on its own but not as a script-dispatched message is typically a symptom of that sort of problem.

January 06 (2 years ago)
keithcurtis
Forum Champion
Marketplace Creator
API Scripter


JP G. said:

Thank you Keith, 

Token-mod command still isn't firing I'm afraid.

I'm using:

!crl \\edit,name=Listener1,text=##,tempate=atk atkdmg,roll=critical \\!token-mod --set currentside|* --ids -NL2sdlukxO4tH3DO_Pg \\CRITICAL HIT

The critical hit text comes up, but the token isn't switching sides.
Running a macro/command line outside of the listener does switch the sides. So it is working on its own, just not in the listener.

If this is something that isn't meant to work, I'm OK with that.

What if you set up the token-mod command as a macro and had CRL call the macro instead of give the token-mod command? I'll reactivate CRL this evening (I use Condefinitions for what I used to use CRL for) and see if I can't figure out a solution.


January 06 (2 years ago)

Edited January 06 (2 years ago)

Apologies Tim, I thought that may have been optional. I tried with what I believe is my player ID but no change in function.

Keith, unless I have my syntax wrong, the following

!crl \\edit,name=Listener1,text=##,tempate=atk atkdmg,roll=critical \\#Crit-Fail-Melee 

results in !token-mod --set currentside|* --ids -NL2sdlukxO4tH3DO_Pg in the Listener Settings panel that pops up after issuing the listener command. So it looks like it's just using the macro to populate the commands not executing the macro independently.  In either event, it doesn't appear to be working either.

I don't want you guys to waste any time on what is essentially a cosmetic game piece, something just saving a click. If you find it an interesting problem however, then I certainly appreciate the help.

I was also trying to get it to roll table results and forget about the token to just provide automatic crit/fumble chat results but to no avail. I was trying the below but I guess the Listener can't do any rolls.

!crl \\edit,name=Listener2,text=##,tempate=atk atkdmg,roll=fumble \\&{template:traits} {{description=**Critical Fail! ** [[1t[Crit-Fail]]]}}