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

Determining status on a token?

So I have been messing around with the api and I needed a function to determine is a specific status was set on token. Here is what I came up with but it seems sort of clunky...am I missing some documentation or something on helper methods to do this? Seems like lots of people might want to do this? I saw "libTokenMarkers" but that seems to deal with token_markers which appears to the full list allowed? I am also sure that this code is not full proof yet but I thought I would ask before I spent to much time in it function isTokenStatusSet(selected,status) { var statusSet = ""; var statusName; var isStatusSet; if(selected && selected.length === 1) { var selected_id = selected[0]._id; var token = getObj('graphic',selected_id); if(token) { statusSet = token.get("statusmarkers").split(","); for (index = 0; index < statusSet.length; index++) { statusName = statusSet[index].split("::"); if (statusName[0] == status){ isStatusSet=true; } } } } return isStatusSet; }
1625601420

Edited 1625601522
The Aaron
Roll20 Production Team
API Scripter
If you did use libTokenMarkers, you could do it like this: !has-status some-status Code: /* global libTokenMarkers */ on('ready', ()=>{ // Make sure libTokenMarkers exists, and has the functions that are expected if('undefined' === typeof libTokenMarkers || (['getStatus','getStatuses','getOrderedList'].find(k=> !libTokenMarkers.hasOwnProperty(k) || 'function' !== typeof libTokenMarkers[k] )) ) { // notify of the missing library sendChat('',`/w gm <div style="color:red;font-weight:bold;border:2px solid red;background-color:black;border-radius:1em;padding:1em;">Missing dependency: libTokenMarkers</div>`); } else { const tokenHasStatus = (token, status) => { let statusObj = libTokenMarkers.getStatus(status); return (false !== token.get(`status_${statusObj.getTag()}`)); }; // active code on('chat:message',(msg) => { if('api'===msg.type && /^!has-status/i.test(msg.content)){ let args = msg.content.split(/\s+/).slice(1); let tokens = (msg.selected || []) .map(o=>getObj('graphic',o._id)) .filter(g=>undefined !== g) .filter(g=>tokenHasStatus(g,args[0])) ; sendChat('',`<div><h4>Tokens With Status ${args[0]}<h4><ul>${tokens.map(t=>`<li>${t.get('name')}</li>`).join('')}</ul></div>`); } }); } }); This is a complete script, but the part doing the work is bolded above. Tokens have a property for each status that's set, named status_<TAG>.  So, something called BlueCow might be status_BlueCow::12351346 or similar.  libTokenMarkers makes it easy to ignore what that tag name is.
Very nice! Thanks much simpler...I need to work on my javascript skill :) 
1625614601
The Aaron
Roll20 Production Team
API Scripter
=D No problem!