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

Generic on change handler

I have some characteristics, let's say STR and INT but there will be more, which get summed up from a few attributes, but most importantly for this question "attr_str_base" and "attr_int_base".  Whenever someone changes the "base" I want to update the associated xpcost, so "attr_str_xpcost" and "att_int_xpcost".  I can handle the change for one of them: on("change:str_base", function() {    getAttrs(["str_base"], function(pvalue) {    var newXpCost = calcAttributeCost(parseInt(pvalue.str_base), 30);    setAttrs({str_xpcost: newXpCost});    }); }); But I would really like to be able to handle them all in one "on("change:whatever") handler. I think I've got about half of the concept down but I'm missing part of it.  I can use the "eventInfo" object to find out which attr changed. on("change:str_base change:int_base *etc*", function(eventInfo) {    getAttrs([eventInfo.sourceAttribute], function(pvalue) {    var newXpCost = calcAttributeCost(parseInt(********), 30);    setAttrs(*******);    } But how do I get the actual value from "pvalue" if I don't know the name of the attr? And how can I construct an object with a property that has a name of the eventInfo.sourceAttribute and the value of newXpCost.  Something like "{eventInfo.sourceAttribute: newXpCost}"? Should I be using repeating structures for this?  Or is there some other way I should go about this? I'm a newb to roll20 character sheets and have some javascript experience but probably only enough to make myself dangerous.  If this isn't the best place for this kind of question please let me know where I should go.
1515320111
GiGs
Pro
Sheet Author
API Scripter
You could do something like this (untested, copied from a recent thread for a similar question): ['str', 'int', 'dex', 'con'].forEach(function (name) {   on("change:" + name + "_base", function () {     getAttrs([name + "_base"], function(values)) {       const n = parseInt(values[name + "_base"])||0; var newXpCost = calcAttributeCost(n, 30);    setAttrs({ [name + "_xpcost"]: newXpCost });     });   }); });
Thank you very much!  That seems to have done it.  There was one extra close-paren after "function(values))", but otherwise nicely done.  Thanks!
1515371547
GiGs
Pro
Sheet Author
API Scripter
Oh yes, well spotted. Glad to have helped!