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

on("change:campaign:turnorder") firing twice

I have created the following test script function DoSomething(){     sendChat('Crawler','DoSomething triggered'); } on("change:campaign:turnorder", function(obj){         sendChat('Crawler','on event triggered');         DoSomething(); }); When I add tokens manually to the turnorder and click to advance turn for the first time, my on Change Event fires twice. Then when I further adavance the turn, it fires once. Why does it fire twice the first time and what can I do to prevent this. I would like DoSomething to fire only when I click on the button to advance the turn.
1590152611
GiGs
Pro
Sheet Author
API Scripter
My guess is its triggering once because a character is added to the turnorder (which immediately changes the turnorder), and then again for the advance turn. You can probably prevent this, but I cant offer suggestions without knowing what you intend the doSomething script is intended to do. You can use this to examine the turnorder contents: on("change:campaign:turnorder", function(obj){         sendChat('Crawler',JSON.stringify(obj));         log(JSON.stringify(obj)); });
1590153309

Edited 1590157121
I just want DoSomething to send something to the Chat to show it has been triggered. the on event fires everytime I add a token to the turn order but, once I have finished adding the tokens, when I press the advance turn button, it fires twice but only once after that. I will play with the turnorder content using the script you kindly provided.
1590154189
GiGs
Pro
Sheet Author
API Scripter
One way to solve the problem would be to check if the active character has changed, sent to chat.  You can do on("change:campaign:turnorder", function(obj, prev){ with prev being the state before the change. Get the active character from both, and if it has changed, then send your message to chat.
1590156121
GiGs
Pro
Sheet Author
API Scripter
Here's a quick-and-dirty expansion to your script: function DoSomething(){ sendChat('Crawler','DoSomething triggered'); } on("change:campaign:turnorder", function(obj, prev){ const neworder =Campaign().get("turnorder") ? JSON.parse(Campaign().get("turnorder")) : []; const oldorder = prev.turnorder ? JSON.parse(prev.turnorder) : []; const newid = (neworder.length > 0) ? neworder[0].id : ''; const oldid = (oldorder.length > 0) ? oldorder[0].id : ''; if(newid && oldid && (newid != oldid)) { DoSomething(); } }); It gets the id of the first person in the turn tracker, from the current turnorder, and the turnorder just before it changed. It then compares the ids, and if they are different, does your DoSomething. There were a couple of complications: handling when you cleared the turn tracker, or when there was only one person in the tracker. But in my testing, this works now.