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

Can change:token Events Be Suppressed When a New Token is Added?

1600062334

Edited 1600062497
Hi everyone,I'm not concerned it there is no answer to the following, but it would certainly be a "nice to have". I have a number of change:token events that are meant to track players doing things like changing names, status, the bar (circle) values, and other things - I've been playing D&D and wargames with my friends since the 70s and I don't trust them! LOL.  The tokens used in my wargame campaign manager game are not individual characters.   When players need, say, a new platoon, they - or most likely I - will drag one off the journal.  It's like having monsters in the journal.  I think the term is minions . Anyway, here is a simple change event that works well and is the basis of a lot of my other change events. on("change:token:bar3_value", function(obj, prev) { let gm_notes = obj.get("gmnotes"); let ts = new Date(); if (gm_notes !== "") {gm_notes = "<br>" + gm_notes;} gm_notes = "STRENGTH: " + prev["bar3_value"] + " --> " + obj.get("bar3_value") + " " + ts + gm_notes; obj.set("gmnotes", gm_notes); }); (I've been shown the ` ` notation before, but I find it a bit difficult to use due to 25 or more years of entrenched habit :)  Also, I don't use the += to build the gmnotes string because I want to have the last change on top; I've tried =+, but it doesn't work) Whenever a minion/unit is dragged from the journal onto the tabletop, some of the change events, like the one above, fire.  The result is shown below: Note that these values are from the default token of the character in the journal.  I have taken pains to ensure that gm notes are empty before assigning a default token. I'd really love to add something, if possible, to the token change events to prevent them firing when the token is added to the desktop.  players will be changing names of their units, and it's a bit cumbersome to have "NAME: SP Artillery Battery --> Dave's Battery Mon Sep 14 2020 04:54:03 GMT+0000 (Coordinated Universal Time)" plopped directly on top of the above. If there isn't a way, no problem, but if there is, I'd love to know. :)  I could then use a single add event to determine when the token was "born" rather then a mess of change events shown above. -- Tim
1600063642

Edited 1600064533
Pat
Pro
API Scripter
Not an expert, maybe consider the "add graphic" event for set-up? Not sure if you can short circuit the other "change" events - maybe set it up so that a "set" variable is tripped on an add that short-circuits the rest?  var addingFlag = false; on("add:graphic", function(obj) {      addingFlag=true;      // do initialize }); on("change:graphic",function(obj) {     if(addingFlag=true){         addingFlag=false;         return;     }else{         // do normal     } });
1600078543

Edited 1600078587
Correct, it is the add graphic event you are looking for. All tokens are graphics but not all graphics are tokens so some filtering is required. <a href="https://roll20.zendesk.com/hc/en-us/articles/360037772813-API-Events#API:Events-add:graphic" rel="nofollow">https://roll20.zendesk.com/hc/en-us/articles/360037772813-API-Events#API:Events-add:graphic</a>
Pat said: Not an expert, maybe consider the "add graphic" event for set-up? Not sure if you can short circuit the other "change" events - maybe set it up so that a "set" variable is tripped on an add that short-circuits the rest? ; Martijn S. said: Correct, it is the add graphic event you are looking for. All tokens are graphics but not all graphics are tokens so some filtering is required. <a href="https://roll20.zendesk.com/hc/en-us/articles/360037772813-API-Events#API:Events-add:graphic" rel="nofollow">https://roll20.zendesk.com/hc/en-us/articles/360037772813-API-Events#API:Events-add:graphic</a> Thank you both very much.&nbsp; I understand the bit about having the add even after an on("ready", function, but I don't know how to apply a variable in a .js script to a specific token.&nbsp; Can you add property to a token?&nbsp; These will be pulled off the journal onto the map throughout; players will be plotting their moves at different times and when they let me know, I'll be correlating the moves from the two maps (side A and B) afterwards. I might just try to use the add:graphic to put something into the gm notes when a token is taken down and do what Pat has suggested.&nbsp; Will try that now. -- Tim
1600084322
The Aaron
Roll20 Production Team
API Scripter
I think the problem you're running into is the same one I have to deal with in TokenNameNumber, namely that the add:graphic &nbsp;event is accompanied by 2–3 change:graphic &nbsp;events. To ignore those non-player triggered events, I suggest adding the graphic's id to a collection variable along with the time, then check that variable in the change event and ignore the change if it has been less than some threshold of time, perhaps a second or two.&nbsp;
1600085743

Edited 1600085805
The Aaron
Roll20 Production Team
API Scripter
Here's a little script that demonstrates the technique: on('ready',()=&gt;{ const THRESHOLD = 1000; // ignore threshold for change events (in milliseconds) const registry = {}; on('add:graphic',(obj)=&gt;{ // Store the id of newly created graphics objects registry[obj.id]=Date.now(); }); on('change:graphic',(obj,prev) =&gt;{ // check if the changed object has a record if(registry.hasOwnProperty(obj.id)){ // check if the changed object was created within the threshold if( (Date.now()-registry[obj.id]) &lt; THRESHOLD ) { sendChat('', 'Add Graphic Change Event'); return; } else { // remove the record if we're outside the threshold delete registry[obj.id]; } } sendChat('', 'Real Player Change Event'); }); });
1600085823
David M.
Pro
API Scripter
Wow, 2-3 change events? Is it a variable number? When I drag a token to the map from the journal, I only see one. When I do, it seems that the name property of the "prev" object from change:graphic is always blank, while "obj" will have the proper name. I was thinking at first that you could just ignore the change event if the prev object has an empty string name, but that won't work if there are multiple changes firing. Curious what other types of change events are getting triggered on add:graphic , or is it only in certain circumstances? Is there some kind of async weirdness going on that causes an unpredictable sequence of events?
1600086520
The Aaron
Roll20 Production Team
API Scripter
It seems like it's something like: Add: create skeleton object Change 1: flesh out object Change 2: set bar link values Change 3: apply token defaults It used to be a single Add event when a token was added, but it got broken by Steve when he added the Token Defaults stuff.
1600086629
David M.
Pro
API Scripter
Good to know, thanks!
The Aaron said: I think the problem you're running into is the same one I have to deal with in TokenNameNumber, namely that the add:graphic &nbsp;event is accompanied by 2–3 change:graphic &nbsp;events. To ignore those non-player triggered events, I suggest adding the graphic's id to a collection variable along with the time, then check that variable in the change event and ignore the change if it has been less than some threshold of time, perhaps a second or two.&nbsp; Thanks for this and the code.&nbsp; I did try setting a statement in the gm notes on add, but there are up to 4 change events - one for the name, and one for each of the bars that have default values (most tokens have default values for bar 1 and 3, some for just one, and two of them, like the example above, have all three bars with default value).&nbsp; Things seemed to get mixed up and confused. -- Tim
1600089041
The Aaron
Roll20 Production Team
API Scripter
Just to be clear, did this fix your problem, or are you still having it?&nbsp; My understanding was that you only wanted to pick up change events from players, but if you're looking for "the last" change event from adding a graphic, that's a slightly different problem, which can be solved with a debounce function.
The Aaron said: Just to be clear, did this fix your problem, or are you still having it?&nbsp; My understanding was that you only wanted to pick up change events from players, but if you're looking for "the last" change event from adding a graphic, that's a slightly different problem, which can be solved with a debounce function. debounce sounds interesting, but your work has fixed my issue.&nbsp; Very grateful, thanks.&nbsp; This allows me to have a newly added token to the tabletop to just&nbsp; have GM notes saying "ADDED to table top Tue Sep 15 2020 00:20:37 GMT+0000 (Coordinated Universal Time)", instead of up to four on change events.&nbsp;&nbsp; Afterwards, things work as I wished. :)&nbsp; -- Tim
1600130657
The Aaron
Roll20 Production Team
API Scripter
Great! A debounce function is one that can only be called once in a given timeframe. It can either be leading edge (called once and subsequent calls in a given timeframe are ignored) or trailing edge (called once during a given timeframe with the last call taking effect).&nbsp; It's probably easiest to set up with Underscore.js:&nbsp; <a href="http://underscorejs.org/#debounce" rel="nofollow">http://underscorejs.org/#debounce</a>