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
This post has been closed. You can still view previous posts, but you can't post any new replies.

Sheetworker on change event not firing for attributes created with setAttrs(), and there does not seem to be an add event.

1551841570

Edited 1551847432
Chris D.
Pro
Sheet Author
API Scripter
Compendium Curator
NOTE: turns out the description below, and the title of this thread are not the correct stating of the the problem. Please skip to 5th entry for much better description of true problem.  I am not sure if this ought to be in this section or the bug section, but I just traced a big problem with my sheet.  To simplify it: I have sheetworkers that do basically calculateR1 { getAttrs(a, b);       let r1 = a+b;    setAttrs( r1); } calculateR2 { getAttrs(c, 11);     let r2 = c+r1;    setAttrs( r2); } calculateR1 gets called when a or b changes.  calculateR2 gets called when c or R1 changes.  At the beginning they all display the correct (default) values.  However when A or B changes, calculateR1 runs, and sets R1 for the very first time. calculteR2 does NOT run, because R1 did not change, it was created for the very first time! The 2nd time a or b is changed, R1 is changed and that triggers R2.   It seems to me that ether on change ought to be triggered when a value is added by a sheetworker, or there ought to be an on add event.  Is this known behavior? Ought I repost this in the "Bugs section"?
1551841918
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Well, not sure if it's a typo with your copied code, but you've got an error in calculateR2, the 11. Additionally, do you mean that R1 does not exist in the html?
1551844175

Edited 1551844191
Chris D.
Pro
Sheet Author
API Scripter
Compendium Curator
The above is not anywhere near true code, just psuedocode to illustrate the problem. I am working on stripping away the 99.9% of my real code that is not needed to show the problem and will post that shortly.  It is not that it does not exist in the html, it does not exist in the attributes and abilities. Which is to say, a brand new character, roll20 does not STORE any values. if the html says value="1", the system will display a field with a 1 in it, but the 1 is not stored anywhere. If the user changes that value to a "2", then it stores the 2 on the attributes and abilities tab. 
1551845898

Edited 1551846107
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Hmm, I'm not seeing this behavior with a simplest possible reproduction of the setup. Let me know if I misunderstood the issue you were facing with this code: <input name='attr_set_manual' type='number' value='0'> <input name='attr_set_by_worker' type='number' value='0'> <input name='attr_set_by_worker_readonly' readonly type='number' value='0'> <script type='text/worker'>     on('change:set_manual',(event)=>{         setAttrs({set_by_worker:event.newValue*2,set_by_worker_readonly:event.newValue*5});//Sets the two attributes via sheetworker; is not silent     });     on('change:set_by_worker change:set_by_worker_readonly',(event)=>{         console.log(`============================ Worker Change Detected ============================`);         console.log(event);//logs the change event. There are two logs any time that set_manual is changed         console.log(`========================================================`);     }) </script>
1551847146

Edited 1551847636
Chris D.
Pro
Sheet Author
API Scripter
Compendium Curator
OK, got it pinned down much more. I actually simplified it too much, and the problem went away, and I had to hunt for the situation that caused it to come back! The real problem is not like I described above (sorry). I have just now learned that the problem is NOT that it does not run the sheetworker when the attribute is ADDED. It is that it does not run an on-change sheetworker if the change is done during an on-sheet-opened sheetworker event! The problem is only seen when you have a function that runs all of your calculations when the sheet is first opened.  <!DOCTYPE html> <meta charset="UTF-8"> <div class="user-root"> <input name="attr_a" type="number" value="1" style="width: 3em;" > <input name="attr_b" type="number" value="1" style="width: 3em;" > <input name="attr_c" type="number" value="1" style="width: 3em;" > <br> <input name="attr_r1" type="number" value="0" style="width: 3em;" > <input name="attr_r2" type="number" value="0" style="width: 3em;" > </div> <script type="text/worker"> var calculateR1 = function () { 'use strict'; log( "r1"); getAttrs(["a", "b" ], function gar1(values) { 'use strict'; let vals = {}; vals[ "r1"] = parseInt(values[ "a"]) + parseInt( values[ "b"]); log( "a = " + values["a"]); log( "b = " + values["b"]); log( "r1 = " + vals["r1"]); setAttrs( vals); }); }; var calculateR2 = function () { 'use strict'; log( "r2"); getAttrs(["c", "r1" ], function gar1(values) { 'use strict'; let vals = {}; vals[ "r2"] = parseInt(values[ "c"]) + parseInt( values[ "r1"]); log( "c = " + values["c"]); log( "r1 = " + values["r1"]); log( "r2 = " + vals["r2"]); setAttrs( vals); }); }; on("change:a change:b", function () { calculateR1(); }); on("change:c change:r1", function () { calculateR2(); }); on('sheet:opened',function fnOnSheetOpened(){ 'use strict'; log( "sheet opened."); calculateR1(); }); </script> When the sheet is opened for the first time, it is supposed to do all the calculations. Now I knew that because of asynchronous issues, R1 is not going to be correct if calculateR2 is run while calculateR1 is still being processed. But I trusted that when R1 was actually written out, it would generate the event that would cause calculateR2 to run. It is not.  I can't think of how to fix this. calculateR2() can't be in sheet-opened, because it would run before calculateR1 is done, and R1 does not yet have the correct value. But I don't want to have calculateR1 call it directly, because then it would be called twice in every other circumstance!
1551847942
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Huh, interesting. I'd swear that that used to work. I can confirm that I'm seeing the same behavior. I'd recommend a bug report, or asking modsquad to move your thread to bug reports.
1551851851
Chris D.
Pro
Sheet Author
API Scripter
Compendium Curator
OK, thank you very much Scott.   Since this thread ends up having an incorrect title, lets consider this thread closed and I will open a new one in the Bugs section.   Thank you very much!
Thank you Chris; I'll be closing this thread.