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

Problem with Turnorder (No new token in turntracker after adding an entry with id)

1682679800

Edited 1682680376
Hi everyone, I'm quite new to the subject of Scripting and encountered a Problem while playing with the "Turnorder". I've already tried to use the search function, but I can't find a proper solution, I hope you guys could help me out. Getting inspired by the "Turnorder"- Subsection of the API:Objects Page, I've tried to build something similar. At first i've built some functions to get started. One for create a Turnorder and Open the Turntracker: function createAndOpenTurnOrder(){         var turnorder;         if(Campaign().get("turnorder") == ""){             turnorder = [];         }else{             turnorder = JSON.parse(Campaign().get("turnorder"));         }         Campaign().set("turnorder", JSON.stringify(turnorder));         Campaign().set("initiativepage", true );         return turnorder; } And One for adding an Entry to the turnorder: function addEntryToTurnOrder(turnorder, myid, prio, name, currentpageid){     turnorder.push({            id: ""+myid,            pr: ""+prio,            custom: ""+name,            pageid: ""+currentpageid     });     Campaign().set("turnorder", JSON.stringify(turnorder)); } For testing purposes i'am calling them like this: on("ready", function() {  var currentmap= findObjs({_type: "page", name:"Testmap"})[0];  turnorder= createAndOpenTurnOrder();  addEntryToTurnOrder(turnorder,-1,22,"Sgt. Example", currentmap.get('id')); } This works well, and ive got an new Entry in the Turntracker. But when i'am trying to add an Entry with an specific Id like shown below, it doesn't work. There is no new Entry in the Turntracker: on("ready", function() {   var currentmap= findObjs({_type: "page", name:"Testmap"})[0];   var testchar= findObjs({_type: "character", name:"My Test Char"})[0];   turnorder= createAndOpenTurnOrder();   addEntryToTurnOrder(turnorder,testchar.get('id'),22,testchar.get('name'), currentmap.get('id')); } Can somebody help me out with this one? I've read that this was an issue caused by the update one year, because it made it mandatory to pass the pageid. But i'am already doing that, so i'am a little bit clueless by now how to go on. Have a fantastic day
1682683173
timmaugh
Pro
API Scripter
Hey, Almighty... welcome to the world of scripting. I believe for non-custom entries (which have to have an id of -1), the turnorder requires a token id, not a character id. Someone else can correct me if I'm wrong.
Hello timmaugh :) thanx for the quick answer. I've tried it with the id of the token on the map, but unfortunately that didn't work either
1682687892
David M.
Pro
API Scripter
Are the get('id') 's returning valid ids?  I might be remembering incorrectly, but I thought the read only properties (_id, _type, etc.) need to be referenced in the "get" with their underscores?   addEntryToTurnOrder(turnorder,testchar.get('_id'),22,testchar.get('name'), currentmap.get('_id')); Regardless, you'll probably want to at least put some checking in there that your various findObjs lines actually return something rather than undefined so you don't crash the sandbox.
1682691388
timmaugh
Pro
API Scripter
The IDs are floated up to be accessible properties of the returned object, too... so you don't even have to do get() to retrieve them. You can just do: testchar.id Also, you don't need the underscore before "type" in the findObjs() function. Those can just be: let currentmap= findObjs({ type: "page", name: "Testmap" })[0]; But I do agree with David that you can put some logging in to tell you what you are getting from those functions.
1682692200
David M.
Pro
API Scripter
Ah cool, thx. I should know by now not to trust my memory haha. 
Okay, i've put some logging into the Script and it feels like the ID is okay:  log(testchar.id) returns a String: "-NU7INrn_AgAC_ZTYpQD" When i try to get more Information by using this String-id, it seems to work too:      var testchar_reference= findObjs({type: "graphic", id: "-NU7INrn_AgAC_ZTYpQD"})[0];       log(testchar_reference.get('name')); return the correct Name: "My Test Char" I've also checked the PageId but its seems to be okay too: "-NTCx17zN-B6Zg5oCgJM"
1682705867
David M.
Pro
API Scripter
Oh wait, it looks like you are passing a character id to the turn order. According to the wiki, the id in turnorder should be for a graphic object. <a href="https://wiki.roll20.net/API:Objects#Turn_Order" rel="nofollow">https://wiki.roll20.net/API:Objects#Turn_Order</a>
Okay i've tried it with the ID of the graphic object, but it still doesn't appers in the turntracker :/
Okay another idea to work around my problem. Is there a way to simulate a click on a token?
1682953874
The Aaron
Roll20 Production Team
API Scripter
Here are my functions for managing the turn order, if it helps: /* eslint-disable no-unused-vars */ const getTurnArray = () =&gt; ( '' === Campaign().get('turnorder') ? [] : JSON.parse(Campaign().get('turnorder'))); const getTurnArrayFromPrev = (prev) =&gt; ( '' === prev.turnorder ? [] : JSON.parse(prev.turnorder)); const setTurnArray = (ta) =&gt; Campaign().set({turnorder: JSON.stringify(ta)}); const addTokenTurn = (id, pr, pageid) =&gt; setTurnArray([...getTurnArray(), {id,pr,_pageid:(pageid||(getObj('graphic',id)||{get:''}).get('pageid'))}]); const addCustomTurn = (custom, pr) =&gt; setTurnArray([...getTurnArray(), {id:"-1",custom,pr}]); const removeTokenTurn = (tid) =&gt; setTurnArray(getTurnArray().filter( (to) =&gt; to.id !== tid)); const removeCustomTurn = (custom) =&gt; setTurnArray(getTurnArray().filter( (to) =&gt; to.custom !== custom)); const clearTurnOrder = () =&gt; Campaign().set({turnorder:'[]'}); const packTo = (to) =&gt; [{id:'HEADER',pr:Number.MAX_SAFE_INTEGER},...to].reduce((m,t)=&gt;{ if('-1'===t.id){ m[m.length-1].packed=[...(m[m.length-1].packed || []), t]; return m; } return [...m,t]; },[]); const unpackTo = (pTo) =&gt; pTo.reduce((m,t)=&gt;{ let packed = t.packed||[]; delete t.packed; if('HEADER' === t.id){ return [...packed,...m]; } return [...m,t,...packed]; },[]); const sorter_asc = (a, b) =&gt; ('-1' === a.id || '-1' === b.id) ? 0 : a.pr - b.pr; const sorter_desc = (a, b) =&gt; ('-1' === a.id || '-1' === b.id) ? 0 : b.pr - a.pr; const sortTurnOrder = (sortBy = sorter_desc) =&gt; Campaign().set({turnorder: JSON.stringify(unpackTo(packTo(getTurnArray()).sort(sortBy)))}); /* eslint-enable no-unused-vars */ The sortTurnOrder function will keep all custom turns in the same location relative to the token before them. If I had to guess at the issue with your&nbsp; addEntryToTurnOrder() function, it would be that custom ends up having a value.&nbsp; I have this vague recollection that the turn order is oddly pedantic about a few things. Also, you can access the id of a Roll20 Object off of the object directly: let id = token.id; instead of using the getter: let id = token.get('id'); Also, you mostly don't need to do manual type coercion into a string: turnorder.push({ id: myid, pr: prio, custom: name, pageid: currentpageid }); When you do, using a template literal is often cleaner syntax: let foo = `Something ${somevar}`; Finally, if you're looking to simulate selecting a token so you can do something like issue a rolls to chat for tracker stuff [[23 &amp;{tracker}]], that won't work.&nbsp; Mod scripts can't affect the selected state for players, and even if they could, they don't have their own selection state for chat messages to make use of.
1682953982
The Aaron
Roll20 Production Team
API Scripter
BTW, if you're looking to dig into Mod (API) scripts and Javascript more, here are some links to discussions that might be helpful: <a href="https://app.roll20.net/forum/post/6605115/namespaces-novice-seeks-help-exploring-the-revealing-module-pattern" rel="nofollow">https://app.roll20.net/forum/post/6605115/namespaces-novice-seeks-help-exploring-the-revealing-module-pattern</a> <a href="https://app.roll20.net/forum/post/6584105/creating-an-object-that-holds-specific-character-dot-id-and-character-name/?pagenum=1" rel="nofollow">https://app.roll20.net/forum/post/6584105/creating-an-object-that-holds-specific-character-dot-id-and-character-name/?pagenum=1</a> <a href="https://app.roll20.net/forum/post/6237754/slug%7D" rel="nofollow">https://app.roll20.net/forum/post/6237754/slug%7D</a>
Even simple macros to put tokens into the turn order have broken lately. Roll20 may have done something on their end that keeps the game from tracking a selected token
1682961941
Gauss
Forum Champion
BigNorseWolf said: Even simple macros to put tokens into the turn order have broken lately. Roll20 may have done something on their end that keeps the game from tracking a selected token I haven't seen that be the case, could you supply a macro that is broken?&nbsp;
Okay, with the functions from The Aaron it works. To be honest i don't understand every line of your code by now, but it is a good point to start. So thank you a lot. I will take your advice and follow the links to learn more :) Thank you everyone for helping me out - i feel really excited to become a part of the mod community ^^