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] Cursor through all tokens on page and run command

1589226059
Giger
Pro
API Scripter
I have a turn announcer script, that announces turn orders.  Part of this script is adding a "green dot' to the token who's turn it is. The problem is, because the turn tracked is cleared using GroupInitiative, the dots are not being removed.  So when I run the clear, I want to cursor through all tokens on the page and remove the green status dots. The script below works, but because i use "msg.selected" i have to select all the tokens first, before the dots would clear out. What i'd like is to replace 'msg.selected' with the array of playerIDs  I believe the allPlayerIDs capture that (I don't know how to print the string out to confirm though). So i need to update the _.each to parse through the string, so I don't have to manually select the tokens myself. // Used for GroupInitiative Clear function, to remove green dots placed by this script. if (msg.content.split(" ")[0] == "!rdot") { var players = findObjs({_type: "player"}); var allPlayerIDs = players.map(function(player) { return player['id']; }); _.each(msg.selected, function(objInfo) { var obj = getObj(objInfo._type, objInfo._id); if( obj.get("_type") == "graphic" ){ if( obj.get("_subtype") == "token" ){ obj.set({ status_green: false }); } } }); }
1589230146

Edited 1589230187
The Aaron
Roll20 Production Team
API Scripter
There are ways to do that which i can give you as example, but you might find it easier to simply observer GroupInitiative and remove the dot when it clears the turnorder, something like this (completely untested) code: /* global GroupInitiative */ if('undefined' !== typeof GroupInitiative && GroupInitiative.ObserveTurnOrderChange){ GroupInitiative.ObserveTurnOrderChange((obj,prev)=>{ if('[]' === obj){ JSON.parse(prev||'[]') .map(o=>getObj('graphic',o.id)) .filter(t=>undefined !== t) .forEach(t=>t.set('status_green',false)) ; } }); }
1589233763
Giger
Pro
API Scripter
Dude! I didn't even know that was possible! That's amazing, it worked perfectly!  I've ran through my use cases, and i haven't had any problems so far. Would you be interested in building the announcer I have into GroupInitiative?  I've been using the two in tandem for years now - they work well hand in hand (I just hate having the turn tracker open)
1589236371
The Aaron
Roll20 Production Team
API Scripter
Hmm.  Probably better as a client of GroupInitiative than included.  You can use that same observer to do other things, as it sends a notification whenever it makes any changes to the TurnOrder, for example adding a green dot on all tokens that are in the turn order: /* global GroupInitiative */ if('undefined' !== typeof GroupInitiative && GroupInitiative.ObserveTurnOrderChange){ GroupInitiative.ObserveTurnOrderChange((obj,prev)=>{ if('[]' === obj){ JSON.parse(prev||'[]') .map(o=>getObj('graphic',o.id)) .filter(t=>undefined !== t) .forEach(t=>t.set('status_green',false)) ; } else { JSON.parse(prev||'[]') .map(o=>getObj('graphic',o.id)) .filter(t=>undefined !== t) .forEach(t=>t.set('status_green',true)) ; } }); } As long as you have GroupInitiative configured to not Auto-open Initiative, you should have your wish regarding not having the turnorder window up.
1589240312
Giger
Pro
API Scripter
Sounds fair.  Thanks again for all your help.  I'm going to tinker with your added code and see if i can get it work for me.  All the best.
1589242173
The Aaron
Roll20 Production Team
API Scripter
No problem!