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

Does "Sheet:Opened" allow for cascading of attributes?

I have a setDefault function to automatically set default attribute values(When blank/empty) for a character (strength, dexerity, etc.) inside a sheet opened event. on("sheet:opened", function() { SheetWorkerFunctions.setDefaults(); //... }); The setDefault function works but it's not triggering cascades when the attributes are set. If I change the attribute on the sheet directly, the cascades function as expected and I don't have the silent value set so I'm not sure why this wouldn't trigger a cascade.
1582109661

Edited 1582109720
GiGs
Pro
Sheet Author
API Scripter
I would expect it to work. Could you show your setDefaults function? As an alternative, why not simply set default values in the html, so you don't need to do this with a sheet worker?
    // Set any value not already set to a default value static setDefaults(default_value = 8) { // Grab every base attribute var attr_values = Object.keys(AT_MAP); getAttrs(attr_values, (values) => { var attr_dict = {}; // Iterate through the attribute values and set default if field is non-existent or empty string attr_values.forEach((attr) => { let this_attr = parseValues(values, attr, "str"); if(!this_attr || this_attr.length == 0) { attr_dict[attr] = default_value; } }); setAttrs(attr_dict); }); } I have some attributes repeated in multiple places(tabs) on the sheet and I didn't want to set them all manually.
1582134241

Edited 1582134547
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
It should Cascade them, although I'm going to give my usual advice; avoid cascading setAttrs whenever possible. The cascade method increases your process time for a given change by 10-100x over just doing it all in a single getAttrs/setAttrs. Additionally having multiple Cascades can cause the sheet to not update in the order you thought it would. Edit: my full post on the subject can be found here
1582134717
GiGs
Pro
Sheet Author
API Scripter
I cant see anything wrong there, but you also have  var attr_values = Object.keys(AT_MAP); and parseValues(values, attr, "str"); i have no way to judge those. What i suggest is to pick exactly one attribute that is lacking a default, ensure your AT_MAP object only has that attribute in it, and test your worker. Also, use some console.log statements, to see the values each variable is being set to, to see if there's something funny going on.
1582134901
GiGs
Pro
Sheet Author
API Scripter
I almost commented earlier "here's some advice to do it, but Scott will be along shortly to tell you why you shouldn't do it anyway." (Joking aside, I'm actually agreeing with you here.)
1582136080
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Heh, this advice has sort of become my signature ;) Also, for the actual question at hand, do your listeners properly trigger when you change one of the attributes? And what does the code that should be triggered look like?
1582143621
Andreas J.
Forum Champion
Sheet Author
Translator
Scott C. said: It should Cascade them, although I'm going to give my usual advice; avoid cascading setAttrs whenever possible. The cascade method increases your process time for a given change by 10-100x over just doing it all in a single getAttrs/setAttrs. Additionally having multiple Cascades can cause the sheet to not update in the order you thought it would. Edit: my full post on the subject can be found here Scott, I added a link to your reply/post on sheetworker optimization to the sheetworker-related pages on the wiki.
I tried setting a strength by itself(setAttrs({'strength':8})) in "sheet:opened" and that still doesn't trigger a cascade. I'm probably going to set them to defaults manually but I'll post a small bit of two sections that should interact. Maybe, the way I've written the events has isolated them from one another. // Check if our sheet is opened for the first time on("sheet:opened", function(){ SheetWorkerFunctions.setDefaults(); }); // Strength Calculations on("change:strength change:strength_percentile", function () { getAttrs(["strength", "strength_percentile"], function (values) { // Parse our variables into number format var stat_str = parseValues(values, "strength"); var stat_str_per = parseValues(values, "strength_percentile"); // Special check for perfect strength if (parseValues(values, "strength_percentile", "str") == "00") stat_str_per = 100; // Set the attributes on the character sheet setAttrs({ "force_open_door": AT_STR.getStrengthValue('Minor', stat_str, stat_str_per), "force_open_door_locked": AT_STR.getStrengthValue('Minor', stat_str, stat_str_per, true), "bend_bars": AT_STR.getStrengthValue('Major', stat_str, stat_str_per), "str_bonus_attack": AT_STR.getStrengthValue('Attack', stat_str, stat_str_per), "str_bonus_damage": AT_STR.getStrengthValue('Damage', stat_str, stat_str_per), }); }); }); These events are in global scope.
1582154444
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Those certainly should cascade, I'll do some testing after dinner
1582186441
GiGs
Pro
Sheet Author
API Scripter
The problem is likely to be in the functions you havent shown us - like parseValues and getStrengthValue. Try not using those functions and just code their contents into the sheet worker-  just for testing purposes. Also use console.log statements to see what the values of those variables are, so you can see what exactly is happening (and if the worker is even being triggered in the first place). I will point out though: the amount of time spent trying to get this working, you'd easily have finished entering the default scores in the html, boring as it is. Though since there does seem to be a problem, it's worthwhile figuring out why it isnt working.
I do want to know why this is occurring so I can avoid writing whatever scripting error I am making in future events/functions assuming that this is not a Roll20 bug of some sort. The parseValues function was taken from the sheetworker wiki.&nbsp; <a href="https://wiki.roll20.net/Sheet_Worker_Snippets" rel="nofollow">https://wiki.roll20.net/Sheet_Worker_Snippets</a> const parseValues = (values, stat, type='int') =&gt; { if(type === 'int') return parseInt(values[stat],10)||0; else if(type === 'float') return parseFloat(values[stat],10)||0; else if(type === 'str') return values[stat]; }; I also commented out the old cases and replaced them with two very basic cases. No cascading results. on("sheet:opened", function() { setAttrs({'strength': 8}); }); on("change:strength change:strength_percentile", function () { getAttrs(["strength", "strength_percentile"], function (values) { // Parse our variables into number format var stat_str = parseValues(values, "strength"); var stat_str_per = parseValues(values, "strength_percentile"); // Set the attributes on the character sheet setAttrs({ "force_open_door": 10, "force_open_door_locked": 20, "bend_bars": 30, "str_bonus_attack": 40, "str_bonus_damage": 50, }); }); });