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] Change Token for multiple bars

April 04 (8 years ago)
Hey everyone, I'm having a small problem getting a script to do exactly what I want. I'm tracking two separate values in Bar 3 and Bar 2. When either of those changes I want the script to run I have it working with one value.

(function(){
on("change:token:bar3_value", function(obj,prev) { //CODE HERE   });  }());

But I can't seem to figure out a way to add the second bar condition (i.e. something like below):

(function(){
on("change:token:bar3_value" || "change:token:bar2_value", function(obj,prev) { //CODE HERE   });  }());

I'd prefer the script not run anytime a token changes as I only need it to when those bars are changed. Any thoughts?
April 04 (8 years ago)
The Aaron
Pro
API Scripter
Extract method on your "Code Here" and call it from both event registrations:

(function(){
  "use strict";
  var handleBarChanges = function(obj,prev){
    //CODE HERE
  };

  on('change:token:bar2_value',handleBarChanges);
  on('change:token:bar3_value',handleBarChanges);
}());

April 04 (8 years ago)
Lithl
Pro
Sheet Author
API Scripter
You can also just register the event for change:token and compare obj and prev parameters to see if a bar changed.
April 05 (8 years ago)
Both great ideas. I was making things way too complicated. Thank you both for your help!
April 05 (8 years ago)
The Aaron
Pro
API Scripter
No problem! =D