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

April 20 (4 years ago)

Edited April 20 (4 years ago)
Timothy Y.
Sheet Author

Re-opening this thread from a year ago as I just encountered it today.  I initialize a few attributes when opening a sheet through the sheet:opened event.  On:change listeners on skill fields connected to those attributes work just fine when the attributes are changed through user interaction.  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.

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


    // Stats to initialize with a value of 1
    const statListOne = [
      'brawn',
      'agility',
      'intellect',
      'cunning',
      'willpower',
      'presence'
    ];
  
    statListOne.forEach((stat) => {
      on('sheet:opened', () => {    
        getAttrs([stat], (values) => {
          const thisStat = (isNaN(values[stat]) || (values[stat]) === 0  || (values[stat]) === "0") ? 1 : parseInt(values[stat]);
          const statValue = thisStat > 0 ? thisStat : 1;
          setAttrs({[stat]:statValue});  // Properly sets the stats, but does not fire an on:change event; bug reported
        });
      });
    });


  const skillStat = {A BUNCH OF skill:stat PAIRS}

  for (const skill in skillStat) {
    on('change:brawn change:agility change:intellect change:cunning change:willpower change:presence', (eventInfo) => {
      const stat = eventInfo.sourceAttribute;
      if (skillStat[skill] === stat) {
        CalculateDiceStatChange(eventInfo,skill,stat);  // Goes on to do the calcs on a manual change, but not from the sheet:opened event
      }
    });  
  };



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.

April 21 (4 years ago)
Timothy Y.
Sheet Author

Thanks Rabulias.  In this case that doesn't seem to be the problem.  I use this same pattern in several of my sheet workers and it seems to work fine.  And the Universal Sheet Worker examples have a bunch of on:change events inside of for loops.

https://wiki.roll20.net/UniversalSheetWorkers

The first function works and properly sets the stats.  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.  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.  Everywhere else when setAttrs() is called, a change event happens on the fields changed.  Mine is just one example, but it happens with any setAttrs() inside a sheet:opened event.  Seems like an oversight on the Roll20 end that can be corrected.

April 22 (4 years ago)

Edited May 29 (4 years ago)
Timothy Y.
Sheet Author

Here is a very basic example that anyone can enter into a custom sheet sandbox:

<input type="numbername="attr_statvalue="0" />

<script type="text/worker">
  on('sheet:opened', () => {
    setAttrs({'stat': 1})
    });
  
    on('change:stat', (eventInfo) => {
      console.log(eventInfo);
    });
</script>


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).  When you change the number field, you can see the console message with the eventInfo.  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.


April 24 (4 years ago)
Timothy Y.
Sheet Author

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.  Has anyone else had this issue with sheet:opened?