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

[Help] msg.selected is empty after selecting a target

1522650292

Edited 1522650948
Tad A.
Pro
Sheet Author
I am writing some custom scripts, and I make heavy use of msg.selected to perform actions on all selected tokens. This generally works great, but I have discovered that if an api invocation includes "@{target|...|token_id}", msg.selected is empty in the on('chat:message') handler. Notably, in the UI the selection highlight around the selected tokens disappears during targeting, and does not reappear until after the chat message is processed. Presumably this is the source of the bug. function command1(selected) { ... } function command2(selected, target) { ... } // Example 1 // Calling "!my_script" // executes command1 // on each selected token. on('chat:message', msg => { msg.selected.map(x => getObj(x._type, x._id)).forEach(command1); }); // Example 2 // Calling "!my_script @{target|MyTarget|token_id}" // should execute command2 // on each selected token // with the chosen target. on('chat:message', msg => { let args = msg.content.split(' '); let target = getObj('graphic', args[1]); msg.selected.map(x => getObj(x._type, x._id)).forEach(selected => command2(selected, target)); }); Example1 above works as expected, but Example2 fails because msg.selected is empty. I see this issue was reported here 4 years ago ( old thread ), but there was no response at all. Has anyone found a workaround or a fix for this in the intervening time?
1522656002

Edited 1522656241
Jakob
Sheet Author
API Scripter
There's no real fix for this, msg.selected and @{target} just don't go together. Here's a possible workaround: have the script whisper an API command button to whomever sent the message initially that allows you to select a target (and with the command already including all the IDs that would normally be in msg.selected). It requires an extra click, though. EDIT:  Second workaround, perhaps a bit more elegant: make a wrapper script !caller that whispers an API command button to you with the initial command, but target (and any other @{blah}) already filled in. Example: !caller !my_script @{target|mytarget|token_id} would whisper you an API command button <a href="!my_script -Abc123">Execute command</a> where the ID has already been filled in by the inital execution, but the msg.selected will now be evaluated whenever you actually click the button.
1522728627
Tad A.
Pro
Sheet Author
Thanks for the suggestions (and saving me from  obligatory xkcd ). That's actually pretty similar to how the scripts are already set up. They're each broken into two parts - one API command is executed by the player (by macro), setting the "selected" and "target", which whispers a chat button to the GM containing contextual prompts and an API command to complete the action. Unfortunately I need to know the target and selection before I can generate the prompts to send to the GM, so I can't move either one into the second half, and this issue blocks me from collecting both in the first half. I'm aiming for simplicity and keeping down chat clutter so I'd prefer not to add a third step, but it looks like I'll have to.
1522734363
Jakob
Sheet Author
API Scripter
That's actually pretty similar to how the scripts are already set up. They're each broken into two parts - one API command is executed by the player (by macro), setting the "selected" and "target", which whispers a chat button to the GM containing contextual prompts and an API command to complete the action. I assume you need more than one selected token, so using @{selected|token_id} is not an option?
1522803192
Tad A.
Pro
Sheet Author
That's correct; I had hoped for these commands to work on multiple selected tokens at once. That's just for convenience though - each command is resolved independently in parallel on each selected token. I can restrict the commands to single-selection and manually trigger them for each token. I think I'll employ both solutions; add an extra whispered button in cases where multi-selection is a big time-saver, and assume single-selection otherwise.
1523067968
Chris D.
Pro
Sheet Author
API Scripter
Compendium Curator
Jakob said: There's no real fix for this, msg.selected and @{target} just don't go together.&nbsp; I agree they don't work together as implemented. But I very strongly suspect that is merely because the developer who implemented @{target} Did not bother to make them work together. Lets get a few hundred votes for ... <a href="https://app.roll20.net/forum/post/6202669/when-using-the-at-%7Btarget%7D-syntax-the-system-should-note-what-tokens-are-selected-before-clearing-them-so-it-can-send-that-information-to-the-api" rel="nofollow">https://app.roll20.net/forum/post/6202669/when-using-the-at-%7Btarget%7D-syntax-the-system-should-note-what-tokens-are-selected-before-clearing-them-so-it-can-send-that-information-to-the-api</a>
1523068326
The Aaron
Pro
API Scripter
In fact, they have worked together in the past, and Riley has fixed this before, but further releases broke it.