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

[HELP] setWithWorker triggers the change event but can't find the function that get's run by that event.

1578123831
Noel
Sheet Author
Hi all, I'm having trouble with `setWithWorker`. It sets the value and triggers the event but there's a function call in my event, which works when called by the sheet worker normally but is undefined when called via `setWithWorker`. Any ideas? Here's the function definition in the sheetworker (defined before the event trigger): const update_ability = function ( name ) { console . log ( 'update_ability: ' + name ); let update = {}; getAttrs ([ name + '_rolled' , name + '_advanced' , name + '_tmp' ], ( v ) => { console . log ( v ); update [ name ] = ( parseInt ( v [ name + '_rolled' ]) || 0 ) + ( parseInt ( v [ name + '_advanced' ]) || 0 ) + ( parseInt ( v [ name + '_tmp' ]) || 0 ); console . log ( update ); setAttrs ( update , { silent: false }); }); }; Here's the event trigger which calls the function: on ( "change:PS_rolled" , function ( args ) { console . log ( 'change:PS_rolled' ) update_ability ( 'PS' ); }); Here's the error message from the API Script log: "change:PS_rolled" "ReferenceError: update_ability is not defined" "ReferenceError: update_ability is not defined\n    at Array.eval (eval at messageHandler (evalmachine.<anonymous>:284:6), <anonymous>:518:5)\n    at self.trigger (evalmachine.<anonymous>:79:37)\n    at messageHandler (evalmachine.<anonymous>:287:11)\n    at process.<anonymous> (/home/node/d20-api-server/node_modules/tiny-worker/lib/worker.js:65:55)\n    at emitTwo (events.js:106:13)\n    at process.emit (events.js:194:7)\n    at process.nextTick (internal/child_process.js:766:12)\n    at _combinedTickCallback (internal/process/next_tick.js:73:7)\n    at process._tickCallback (internal/process/next_tick.js:104:9)"
1578124220
GiGs
Pro
Sheet Author
API Scripter
I'm not seeing your code for setWithWorker, which might help. The thing i notice is  on("change:PS_rolled", function(args) { change events should always be entirely lower case, even when the attribute isn't. If you use caps, sometimes it works fine, sometimes it doesnt work at all, and sometimes it causes very weird errors. So the first thing I'd do to eliminate it as a source of error is change that worker to on("change:ps_rolled", function(args) { console.log('change:PS_rolled') update_ability('PS'); });
1578124463

Edited 1578129251
GiGs
Pro
Sheet Author
API Scripter
I remembered just after posting: there's a long-standing bug with eventInfo, when using API scripts to trigger change events, with exactly the symptoms you describe - the eventInfo values are undefined. I'd recommend changing the sheet worker not use eventInfo if you are planning to use it with an APi script, but instead create some kind of universal sheet worker , so the attribute names are set explicitly.
1578144619
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
While Gigs is correct in all those possible issues, I think your problem may actually be hoisting (or rather lack thereof) of your function. Variables defined using const and let aren't hoisted; that is they aren't defined until that line of code is run. Vars are hoisted, which means that regardless of which line a variable defined using var is, it is defined at the beginning of the scope. Depending on how you have your sheetworker code set up, this could be the issue. Can you share the complete sheetworker code?