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

Using getAttrs to get an autocalculated attribute

I have an autocalculated number attribute, "foo_modchars", that simply adds two other fields. In the "on("change:" for yet another field I want to get the value of the autocalculated attribute so I can do some math with it.  "getAttrs(["foo_modchars"]" gives me access to the formula for this attribute but not the value. I asked the other day about passing an autocalculated field into a roll template and apparently I need to surround it with "[[" and "]]" in that case to "parse the formula".  That's not working in the case of "on("change" worker script.  Can I use getObj to do it?  The documentation I saw on that wasn't making sense to me.
1515473477

Edited 1515473696
Your best bet would be to avoid the autocalc fields entirely if you can. Monitor the two other fields and when they change, have a sheetworker that adds them up to the new value. Then you can refer to the new value in other calculations much easier. For example: on("sheet:opened change:stat_a change:stat_b", function() {     getAttrs(["stat_a", "stat_b"], function(values) {         setAttrs({             "foo_modchars": parseInt(values["stat_a"],10) || 0 + parseInt(values["stat_b"],10) || 0         });     }); });
1515478673
GiGs
Pro
Sheet Author
API Scripter
I rememebr seeing a script someone wrote to allow you to use autocalc fields within sheet workers, but it's just simpler to use Rabulias's approach. Add the relevant stats to the on(change:) line, and duplicate the calculation within the sheet worker.  I generally don't put my working in the setattrs call, but before it so i can more easily check it. Something like on("sheet:opened change:stat_a change:stat_b", function() { getAttrs(["stat_a", "stat_b"], function(values) { var stat_a = parseInt(values["stat_a"],10)||0; var stat_b = parseInt(values["stat_b"],10)||0; var output = stat_a + stat_b; setAttrs({ "foo_modchars": output }); }); }); Both approaches are functionally identical, Rabulias's approach is neater. I am just less familiar with javascript, so like to be able to see what's happening more easily. This allows me to add debugging (console.log) statements to examine what's happening when I'm inevitably not getting the expected output :)
Thanks for the replies!&nbsp; This forum is very helpful.&nbsp; I have a real design reason I need to use the autocalc fields in this way, or at least I think I do.&nbsp; I have Characteristics (ie STR, INT, etc) that provide modifiers to skills.&nbsp; I have those modifiers in four fields for each Characteristic (str_modprimary, str_modsecondary, str_modnegsecondary, str_modnegprimary).&nbsp; Any given skill will use a few of these modifiers and I want to be able to set that up in the HTML with autocalcs and then add them to the skill as needed.&nbsp; Maybe there's a better way to do this, but I slept on it and I'm not coming up with something (but I'm a newb to Roll20 sheets!).&nbsp; Hopefully I've explained the situation well. Anyway, some googling led me to what is probably the script G G was mentioning:&nbsp; <a href="https://github.com/Lithl/lithl-snippets/tree/maste" rel="nofollow">https://github.com/Lithl/lithl-snippets/tree/maste</a>... I'll give that a try tonight, and thanks again!
1515574379
Jakob
Sheet Author
API Scripter
Everything that can be done using autocalc fields can be done more elegantly using sheet workers (but that doesn't mean you have to do it if you don't know how).
I actually backed up and took a slightly different approach last night.&nbsp; I was trying to have one generic "on(change:" thing handling all of my Characteristics and Skills (it's a homebrew d100 game like Runequest or BRP, but the chars are also d100), but that wasn't really appropriate for other reasons.&nbsp; I'm breaking it up into a few categories of different kinds of things that have their own generic handlers.&nbsp; I'm getting used to how this works and feeling out the design of what I want to do, thanks to the help I've gotten here! Having getAttrs require a callback takes some getting used to for me (as I'm not primarily a javascript developer), but I think I'm getting the idea.
1515612004
Lithl
Pro
Sheet Author
API Scripter
Jakob said: Everything that can be done using autocalc fields can be done more elegantly using sheet workers I disagree. Personally, I think something like "@{acrobatics-ranks} + @{dexterity-mod} - @{armor-penalty}" is much more elegant than getAttrs(['acrobatics-ranks', 'dexterity', 'armor-penalty'], (values) =&gt; { setAttrs({ acrobatics: parseInt(values['acrobatics-ranks'], 10) + Math.floor((parseInt(values.dexterity, 10) - 10) / 2) - parseInt(values['armor-penalty']), }); }); Certainly, sheet workers can do &nbsp;everything that autocalc can do (and more), but that's not the same as "more elegant".
1515616081
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
True, but then, to interact with that field for anything set via sheetworkers, you need a very ungainly recursive calculator for what the value currently is.