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

Sheetworker on-change events not firing when changes are made within a routine called by sheet:opened event

1587418153

Edited 1587419725
Timothy Y.
Sheet Author
Re-opening this thread from a year ago as I just encountered it today.&nbsp; I initialize a few attributes when opening a sheet through the sheet:opened event.&nbsp; On:change listeners on skill fields connected to those attributes work just fine when the attributes are changed through user interaction.&nbsp; However, the change made to the attribute by the function in the sheet:opened event does not fire the on:change event, and so does not update the connected skills. <a href="https://app.roll20.net/forum/post/7266026/sheetworker-on-change-events-not-firing-when-changes-are-made-within-a-routine-called-by-sheet-opened-event/?pageforid=7268028#post-7268028" rel="nofollow">https://app.roll20.net/forum/post/7266026/sheetworker-on-change-events-not-firing-when-changes-are-made-within-a-routine-called-by-sheet-opened-event/?pageforid=7268028#post-7268028</a> &nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;Stats&nbsp;to&nbsp;initialize&nbsp;with&nbsp;a&nbsp;value&nbsp;of&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp; const &nbsp; statListOne &nbsp;=&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' brawn ' , &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' agility ' , &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' intellect ' , &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' cunning ' , &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' willpower ' , &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' presence ' &nbsp;&nbsp;&nbsp;&nbsp;]; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; statListOne . forEach ((stat)&nbsp; =&gt; &nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; on ( ' sheet:opened ' ,&nbsp;()&nbsp; =&gt; &nbsp;{&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getAttrs ([ stat ],&nbsp;(values)&nbsp; =&gt; &nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const &nbsp; thisStat &nbsp;=&nbsp;( isNaN ( values [ stat ])&nbsp;||&nbsp;( values [ stat ])&nbsp;===&nbsp; 0 &nbsp;&nbsp;||&nbsp;( values [ stat ])&nbsp;===&nbsp; " 0 " )&nbsp;?&nbsp; 1 &nbsp;:&nbsp; parseInt ( values [ stat ]); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const &nbsp; statValue &nbsp;=&nbsp; thisStat &nbsp;&gt;&nbsp; 0 &nbsp;?&nbsp; thisStat &nbsp;:&nbsp; 1 ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; setAttrs ({[ stat ]: statValue });&nbsp;&nbsp; //&nbsp;Properly&nbsp;sets&nbsp;the&nbsp;stats,&nbsp;but does&nbsp;not&nbsp;fire&nbsp;an on:change&nbsp;event;&nbsp;bug&nbsp;reported &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp; const &nbsp; skillStat &nbsp;=&nbsp;{A BUNCH OF skill:stat PAIRS} &nbsp;&nbsp; for &nbsp;( const &nbsp; skill &nbsp;in&nbsp; skillStat )&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp; on ( ' change:brawn&nbsp;change:agility&nbsp;change:intellect&nbsp;change:cunning&nbsp;change:willpower&nbsp;change:presence ' ,&nbsp;(eventInfo)&nbsp; =&gt; &nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const &nbsp; stat &nbsp;=&nbsp; eventInfo .sourceAttribute; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if &nbsp;( skillStat [ skill ]&nbsp;===&nbsp; stat )&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CalculateDiceStatChange ( eventInfo , skill , stat );&nbsp;&nbsp; //&nbsp;Goes&nbsp;on&nbsp;to&nbsp;do&nbsp;the&nbsp;calcs&nbsp;on&nbsp;a&nbsp;manual&nbsp;change,&nbsp;but&nbsp;not&nbsp;from&nbsp;the&nbsp;sheet:opened&nbsp;event &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;});&nbsp;&nbsp; &nbsp;&nbsp;};
Someone with more skill points in Javascript than me will be along soon, and I could be wrong, but I do not think you can have your on() command inside a for . The sheetworkers respond to events so the on() needs to happen first to run anything.
Thanks Rabulias.&nbsp; In this case that doesn't seem to be the problem.&nbsp; I use this same pattern in several of my sheet workers and it seems to work fine.&nbsp; And the Universal Sheet Worker examples have a bunch of on:change events inside of for loops. <a href="https://wiki.roll20.net/UniversalSheetWorkers" rel="nofollow">https://wiki.roll20.net/UniversalSheetWorkers</a> The first function works and properly sets the stats.&nbsp; The second function works and sets the associated skill value when a user directly interacts with one of the stat fields by typing in or using the browser's number arrows (they're all number fields)--that definitely triggers an on:change event.&nbsp; It's that the setAttrs() inside the first function that is originally triggered with a sheet:opened event doesn't trigger a change event, which is what the second function detects in order to run.&nbsp; Everywhere else when setAttrs() is called, a change event happens on the fields changed.&nbsp; Mine is just one example, but it happens with any setAttrs() inside a sheet:opened event.&nbsp; Seems like an oversight on the Roll20 end that can be corrected.
1587578605

Edited 1590791690
Timothy Y.
Sheet Author
Here is a very basic example that anyone can enter into a custom sheet sandbox: &lt; input &nbsp; type =" number "&nbsp; name =" attr_stat "&nbsp; value =" 0 "&nbsp;/&gt; &lt; script &nbsp; type =" text/worker "&gt; &nbsp;&nbsp;on('sheet:opened',&nbsp;()&nbsp;=&gt;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;setAttrs({'stat':&nbsp;1}) &nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;on('change:stat',&nbsp;(eventInfo)&nbsp;=&gt;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;console.log(eventInfo); &nbsp;&nbsp;&nbsp;&nbsp;}); &lt;/ script &gt; When you open the sheet, you can see the stat is properly set to 1 (from a default value of zero, or whatever was entered before).&nbsp; When you change the number field, you can see the console message with the eventInfo.&nbsp; However, that console doesn't appear when the sheet is opened and the value is changed to1 via the setAttrs inside the sheet:opened event -- it doesn't seem to fire a change event.
I haven't confirmed this, but I suspect that the sheet:opened event is global in scope, so it doesn't know what character sheet to update when calling a setAttrs.&nbsp; Has anyone else had this issue with sheet:opened?