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 .
×

[Script Request] Priority Tokens to Front

I've tried and failed miserably to make this script myself for the past day and a half. I finally given up :'( I want on change:graphic, for all objects with a set custom attribute to be brought toFront. The attribute I was trying to check was called "priority_yes", with current: 1, which has been set on all my priority tokens. I'm not worried if I have to change this if it helps. Thanks.
1611342056
The Aaron
Roll20 Production Team
API Scripter
See if this works: on('ready',()=>{ const toFrontFixed = (()=>{ let queue=[]; let last=0; const DELAY = 1000; const burndownQueue = ()=>{ let o = queue.shift(); toFront(o); last = Date.now(); if(queue.length){ setTimeout(burndownQueue,DELAY); } }; return (obj=>{ if(queue.length){ queue.push(obj); } else { let t = Date.now(); if(last+DELAY > t){ queue.push(obj); setTimeout(burndownQueue,(last+DELAY-t)); } else { toFront(obj); last = t; } } }); })(); on('change:page:_zorder',(page)=>{ findObjs({type:'graphic',pageid:page.id}) .filter(g=>g.get('represents').length) .filter(t=>parseInt((findObjs({type:'attribute',characterid:t.get('represents'),name:'priority_yes'})[0]||{get:()=>'0'}).get('current'))>0) .forEach(toFrontFixed) ; }); }); There are a few problems with doing this: 1) What is at the front is not completely under the control of the API, or the client in general.  If a player controls a token, that graphic will always appear for them above anything else. 2) ToFront() has a horrible race condition where calling it on multiple objects will only work on some of them as they each overwrite the same field on the page object.  To avoid this, you have to defer each call with 1000ms between (which is what the toFrontFixed() function in the above does). 3) The order is actually controlled in the page's _zorder field, so observing a graphic you won't see an event for an ordering change. I've not tested the above, but it should work... (crosses fingers), but will take a minimum of 1 second per priority token.
Thanks Aaron, It's not throwing up any errors, however, it also doesn't appear to be doing anything from what I can tell.
1611347505
The Aaron
Roll20 Production Team
API Scripter
Ok, I'll try it out tonight and see if I can get it working.
1611415826
The Aaron
Roll20 Production Team
API Scripter
Hmm.  I tried this out and it worked perfectly based on my understanding of your setup. I created some graphics on a page. I setup a character with an attribute named 'priority_yes' (all lower case) with value 1 When I change the zorder by using "To Front", "To Back", or dragging in a new graphic, the priority_yes == 1 tokens pop to the front. Is it possible your case is running afoul of problem 1 in my first post?  Are you trying to get a bridge or similar graphic to always be above player tokens?  That won't work in all case, as players will always see the tokens they control on top, regardless of what the real zorder is.
Oh yes! It worked on a new drag on drop token from journal. I would like it to work when a token moves if possible. Which I managed to modify. Thanks again Aaron :) Also, yes, the players tokens always appear on top for themselves. However the script still makes it ontop for me however, which is still beneficial. Thanks
1611447008
The Aaron
Roll20 Production Team
API Scripter
Ah, great!