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

Trying to move a character to the bottom of turn order via api macro

1609297380

Edited 1609297422
Mike J
Pro
Marketplace Creator
I am trying to write a custom API to move a character down to the bottom of the turn tracker, regardless of which position they are in.  I get no errors, but when I run the api nothing happens. I am not using "normal' initiative for my 5e campaign.  It is either the players turn or the enemies turn.  Since I have 7 players, I wanted a way for them to click a button that posts a message in chat that they are taking their action, while simultaneously moving them to the bottom of the turn order.  Since the player may not be at the top of the tracker (all players can act at any time), it is a little more challenging to control the array. This was my attempt to just push the Fiadh character to the end of the array regardless of what number in the array she was in: on('ready',function(){   "use strict"; on('chat:message',function(msg){     var turnorder;     if('api' === msg.type && msg.content.match(/^!turn-fiadh/) ){ if(Campaign().get("turnorder") == "") turnorder = []; //Checking to make sure that the turnorder isn't empty. If it is, treat it like an empty array. else turnorder = JSON.parse(Campaign().get("turnorder")); turnorder.push(turnorder.splice(turnorder.indexOf("Fiadh"), 1)[0]); //Shift Fiadh to the end of the turn order no matter where she is at in the array. Campaign().set("turnorder", JSON.stringify(turnorder));     }   }); });
1609298261
The Aaron
Roll20 Production Team
API Scripter
Here's a pretty simple script that will do that: on('ready',()=>{ /* eslint-disable no-unused-vars */ const getTurnArray = () => ( '' === Campaign().get('turnorder') ? [] : JSON.parse(Campaign().get('turnorder'))); const setTurnArray = (ta) => Campaign().set({turnorder: JSON.stringify(ta)}); const addTokenTurn = (id, pr) => setTurnArray([...getTurnArray(), {id,pr}]); const addCustomTurn = (custom, pr) => setTurnArray([...getTurnArray(), {id:"-1",custom,pr}]); const removeTokenTurn = (tid) => setTurnArray(getTurnArray().filter( (to) => to.id !== tid)); const removeCustomTurn = (custom) => setTurnArray(getTurnArray().filter( (to) => to.custom !== custom)); const clearTurnOrder = () => Campaign().set({turnorder:'[]'}); const sorter_asc = (a, b) => a.pr - b.pr; const sorter_desc = (a, b) => b.pr - a.pr; const sortTurnOrder = (sortBy = sorter_desc) => Campaign().set({turnorder: JSON.stringify(getTurnArray().sort(sortBy))}); /* eslint-enable no-unused-vars */ const buildArgs = (str) => { let firstWhitespace = str.search(/\s/); if(-1 === firstWhitespace){ return [str]; } return [str.slice(0,firstWhitespace),str.slice(firstWhitespace+1)]; }; on('chat:message',msg=>{ if('api'===msg.type && /^!turn(\b\s|$)/i.test(msg.content) ){ let args = buildArgs(msg.content); if(args.length>1){ let parts = args[1].split(/\|/); removeCustomTurn(parts[0]); addCustomTurn(parts[0],parts[1]||""); } else { let who = (getObj('player',msg.playerid)||{get:()=>'API'}).get('_displayname'); sendChat('',`/w "${who}" Usage: <code>!turn Some Name|value</code>. For example: <code>!turn Bob the Slayer|23</code>. The value will be blank by default if you use <code>!turn Bob the Slayer</code>.`); } } }); }); You can call it like this: !turn fiadh And it will remove any turn named "fiadh" and add a turn at the end with the name "fiadh".  It is case sensitive. Effectively, you can set up a single macro for each player with that command and it will both roll them in to an empty turn order, and move them to the end when they are done with their turn.  Additionally, if it matters, you can supply something to put in the box like this: !turn Bob the Slayer|23 This will again remove any turn named "Bob the Slayer" and add a turn at the bottom for "Bob the Slayer" with the value 23 in the box. Calling it without any arguments will print out some instructions: !turn I left a bunch of handy functions in the code in case you want to fiddle.  Let me know if that works for you. Cheers!
1609335686
Mike J
Pro
Marketplace Creator
Thanks Aaron, you are AMAZING!