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

Update turntracker value with API?

1404723630

Edited 1404723789
DXWarlock
Sheet Author
API Scripter
Anyone know a way to update the turn tracker pr value with API? I can add to it fine for example with this snippet: order = JSON.parse(Campaign().get("turnorder")); order.push({id: msg.selected[i]._id,pr: rollResult,}); But of course that adds a new instance of the player to the tracker with the new pr value, not update the current one. I cant find a way to do it via API to update an entries pr value..any tips?
1404744898

Edited 1404745423
The Aaron
Roll20 Production Team
API Scripter
( EDIT: fixed the update as it didn't work, but it does now! ) Funny.. I just wrote a snippet of code that does this... here it is (I bolded the turn order bits for you): on('chat:message',function(msg) { if (msg.type !== "api"){ return; } var tokenized = msg.content.split(" "), command = tokenized[0]; switch(command) { case '!init2': if (msg.selected) { var turnorder=Campaign().get('turnorder'); if("" === turnorder) { turnorder = []; } else { turnorder = JSON.parse(turnorder); } _.each(msg.selected, function(o) { var obj = getObj(o._type,o._id), mod = 0, result = 0, init, found; if (obj && 'token' === obj.get('subtype')) { if ("" !== obj.get('represents')) { init = findObjs({ _type: 'attribute', _characterid: obj.get('represents'), name: 'init' })[0]; if ( init ) { mod = Math.round(+init.get('current'),0); } } else { mod=Math.round( (obj.get('bar1_value')/3), 0); } result = randomInteger(20)+mod; found = false; for(var i=0; i < turnorder.length; ++i) { if(obj.id === turnorder[i].id) { turnorder[i].pr=result; found=true; break; } } if(!found) { turnorder.push({id:obj.id,pr:result}); } } }); Campaign().set('turnorder',JSON.stringify(turnorder)); } break; } });
1404750958
Lithl
Pro
Sheet Author
API Scripter
William R. said: Anyone know a way to update the turn tracker pr value with API? I can add to it fine for example with this snippet: order = JSON.parse(Campaign().get("turnorder")); order.push({id: msg.selected[i]._id,pr: rollResult,}); But of course that adds a new instance of the player to the tracker with the new pr value, not update the current one. I cant find a way to do it via API to update an entries pr value..any tips? In your example, order is an array of objects. Array.prototype.push adds an element to the end of the array. However, you can access individual elements with the index operator (eg, order[1] would be the second element in the array), and you can set the values of those elements. You also have access to the Underscore.js library in your API scripts, which among other things makes array manipulation quite easy. You can see examples of both element access and use of the underscore library in Aaron's answer. turnorder[i].pr = result sets the initiative value of a token, while _.each(msg.selected, ...); iterates over the array of selected tokens.
Oooh this is juicy stuff. So you can add timed events using this, yea? If I use a round counter to track what round it is... is this always going to be element 0 in the turn order?
1404781285

Edited 1404781381
DXWarlock
Sheet Author
API Scripter
Ah thanks guys. that will help a lot. I've been manually removing and redoing the !command for them to update their turn, then re-sort,etc..that got old after a few sessions :) And dont think it will be [0] Michael using push it adds it to the end, you could use .unshift instead of .push to put it at the top of the list. But if you sort it will move it its correct place. What i have is: order.unshift({"id": '-1',"pr": 100,"custom": "StartRound"}); as my counter/marker at the top of each round, and since I sort by descending, with it100 its always at the top no matter how many times I update and re-sort.
1404793852
The Aaron
Roll20 Production Team
API Scripter
Michael: element 0 will always be the top of the turn order as it's displayed. If no one is in the turn order, it may not be an array, which is why there is a ("" === turnorder) check.