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]Remove token from the turn order

is there a way to remove a token from the turn order using the api
1409868703
Lithl
Pro
Sheet Author
API Scripter
Yes. Something to this effect: var turnorder; if(Campaign().get("turnorder") == "") turnorder = []; else turnorder = JSON.parse(Campaign().get("turnorder")); turnorder = _.reject(turnorder, function(item) { return item.id == token.id; // Obtaining a reference to the desired token left as an exercise to the reader }); Campaign().set("turnorder", JSON.stringify(turnorder));
1410499540

Edited 1410500548
got it to work and now able to remove a token from the turn order once a token is killed <a href="https://gist.github.com/53c428b52db4f9179a07.git" rel="nofollow">https://gist.github.com/53c428b52db4f9179a07.git</a>
1410535704
The Aaron
Roll20 Production Team
API Scripter
Nice! I do have a few recommendations... You should probably move line 32 [ Campaign().set(...) ] to before line 26. You only need to change the turn order if something died. You should move line 5 [ var turnorder = Campaign().get("trunorder") ] before line 3. This line is overwriting the turnorder variable with the value stored in the campaign object. I'm surprised it is working because it will be a string rather than the array that is created by JSON.parse() and expected on line 22. You can really do all of the turn order business inside the if block on line 17, since that is the only part that deals with turn order.
1410538098
Lithl
Pro
Sheet Author
API Scripter
While your for loop works (and is probably faster by a matter of milliseconds), I recommend becoming comfortable with the Underscore functions, as they can be tremendously useful. Compare: var i = 0; for (i = 0; i &lt; turnorder.length; i++) { if (turnorder[i].id == id) { turnorder.splice(i, 1); } } To: turnorder = _.reject(turnorder, function(item) { return item.id == id; }); Additionally, lines 7-15 can be easily changed to: obj.set('status_red', obj.get('bar1_value') &lt;= obj.get('bar1_max') / 2); While you could do the same thing for setting status_dead, you still need the if block for changing the turn order, so it doesn't gain you as much. Finally, since you're removing status_dead if the token goes above 0 health, you might consider what happens if a token drops to 0 (and is removed from the turn order) and then gets healed/raised and needs to be put back.