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

Weird API / Sheetworker script interaction

1557502781
StéphaneD
Pro
Sheet Author
API Scripter
Hi I have a weird message when I save an API script I have written : "ReferenceError: changeCaracs is not defined" "ReferenceError: changeCaracs is not defined\n    at eval (eval at messageHandler (evalmachine.<anonymous>:284:6), <anonymous>:590:6)\n    at eval (<anonymous>)\n    at messageHandler (evalmachine.<anonymous>:284:6)\n    at process.<anonymous> (/home/node/d20-api-server/node_modules/tiny-worker/lib/worker.js:60: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)" The problem is that 'changeCaracs' does not appear anywhere in the API script ! However, this identifier does exist somewhere in this game, but as a changeCaracs() function that I have defined in the sheetworker section of the character sheet !  Now for the strangest of all, as far as I can tell, the sheetworker on() event handler script that calls this function works as designed, as well as the API script.  I just find this message a bit annoying and thought I would report it, in case someone already encountered a similar situation Thanks in advance
1557504974
GiGs
Pro
Sheet Author
API Scripter
The API sandbox does report errors in sheet workers. It might be that you have an error in the changeCaracs function or something that interacts with it or somes immediately before it in the sheet html file. It could be a minor error that doesnt stop it working. If you share your changeCaracs function we might be able to spot if there's an error in it. Does your API script change any attributes that are part of the sheet worker?
1557505705
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
I'm not sure if this has any bearing: <a href="https://app.roll20.net/forum/post/7440478/api-no-longer-triggering-sheet-workers" rel="nofollow">https://app.roll20.net/forum/post/7440478/api-no-longer-triggering-sheet-workers</a>
1557505774
StéphaneD
Pro
Sheet Author
API Scripter
Hi GiGs Here is the only place in the sheet-worker where the changeCaracs function is used : function changeCaracs() { let changedCaracs = ''; const caracs = ['FORCE', 'DEXTERITE', 'CONSTITUTION', 'INTELLIGENCE', 'PERCEPTION', 'CHARISME', 'NIVEAU', 'DEFARMUREON']; for (let c = 0; c &lt; caracs.length; c++) { changedCaracs += `change:${caracs[c].toLowerCase()} `; } return changedCaracs; } on(changeCaracs(), function(eventInfo) { // on reconstruit d'abord l'attribut caché CARACS... buildCaracs(); // ... puis on met à jour les buffs qui contiennent une référence à la caractéristique modifiée updateBuffs(eventInfo); }); The API script does not set / change any attributes in the character sheet TIA
1557506611
GiGs
Pro
Sheet Author
API Scripter
I dont see anything worrying there, but the buildcaracs and updatebuffs functions might be worth a look too since an error in those might bubble back to the changeCaracs function (maybe...). Also adding a semi-colon at the end of the changeCharacs function (Shouldnt be needed, but you never know...). By the way, what do buildCaracs do and updateBuffs do? i'm surprised you have a function that uses a list of attributes, but those functions apparently dont use those attributes.
1557506665
GiGs
Pro
Sheet Author
API Scripter
i wonder if keith's link is the cause. There might be no issue with the script, it might just be a glitch caused by the current bug.
1557506781

Edited 1557513837
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
I'd probably rewrite your changeCaracs/on('change') combo like this: const caracs = ['FORCE', 'DEXTERITE', 'CONSTITUTION', 'INTELLIGENCE', 'PERCEPTION', 'CHARISME', 'NIVEAU', 'DEFARMUREON']; on(`change:${caracs.join(' change:').toLowerCase()}`,(eventInfo)=&gt;{ // on reconstruit d'abord l'attribut caché CARACS... buildCaracs(); // ... puis on met à jour les buffs qui contiennent une référence à la caractéristique modifiée updateBuffs(eventInfo); }); I think you're running into a problem with how you're calling changeCaracs, and this method just sidesteps that entirely, and is a bit more streamlined (thanks to GiGs for originally showing this method of change generation to me several months ago). EDIT: As for the API/sheetworker interaction bug being the cause. I can tell you that that is not the case. The bug with the integration between the two is that sheetworkers no longer listen to APIs, but unless the sheetworker would have already caused the sandbox to throw an error it won't cause problems.
1557512038
GiGs
Pro
Sheet Author
API Scripter
Hehe, thanks! i was going to make a suggestion of that sort but had to rush away and left it for later. I'm also curious about the buildcaracs and updateBuffs functions, and suspect they could benefit from similar optimisation using the new caracs variable.
1557514428
The Aaron
Roll20 Production Team
API Scripter
I would add a semicolon and carriage return before the changeCaracs function declaration. A reference error will only happen if a symbol is not declared in the scope where it is referenced. &nbsp;The way API scripts are joined can lead to issues when the trailing part of a script doesn't have a semicolon or carriage return, and the beginning of a script declares something important. I'm thinking this is an error when merging the API scripts and the sheet worker "script" which is resulting in the function not getting declared in the api sandbox, despite working in the character sheet.&nbsp; The suggested rewrites are also a good idea, but it would be nice to isolate the issue.&nbsp;
1557515094
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
The changecaracs function is actually a sheetworker though Aaron.
1557515937
The Aaron
Roll20 Production Team
API Scripter
I know, but sheet workers are imported into the API sandbox as an invisible script.&nbsp;
1557561912
StéphaneD
Pro
Sheet Author
API Scripter
All Wow, thanks for all the answers There was already a carriage return -- actually an empty line -- before the function declaration Adding a semicolon at the end of the line above -- which is the closing } of another function declaration -- does not help Doing the rewrite as suggested by Scott addresses the problem with changeCaracs() but there is still a ReferenceError with another function that is written similarly (changeRanks to name it) I have created a Gist with the sheet-worker script, so that you can see the code of all other functions and event handlers <a href="https://gist.github.com/stephaned68/a72494fb26bf5c70ba69959a32bb36fb" rel="nofollow">https://gist.github.com/stephaned68/a72494fb26bf5c70ba69959a32bb36fb</a> If you need anything else, just let me know Thx Stéphane PS : Be kind, guys, I have been a Unix then Windows developer for the last 30 years but haven't done any real web development work, and after being laid off, I thought I would use Roll20 and my RPG hobby to try and learn some Javascript coding by myself, so it may not be the most streamlined and optimized ES5/ES6 code ;)
1557572351
The Aaron
Roll20 Production Team
API Scripter
No worries.&nbsp; Roll20 API is a great place to learn some Javascript.&nbsp; Prior to using the API, the most Javascript id did was "disable web button when it's clicked to prevent double submit of a form."&nbsp; After a few years of working on Roll20 API scripts, I was able to turn create and deploy a full Chrome Extension for one of the VPs at my job in 18 clock hours. =D&nbsp; I highly recommend Javascript: The Good Parts by Douglas Crockford as a find grounding in writing good javascript.&nbsp; It's prior to ES6, but I think it's still an important read.&nbsp; If you're like me, wrapping your brain around closures and asynchronous code will be the two big challenges. As you can see, we're a pretty helpful bunch, so feel free to ask whatever questions you want. =D
If you're like me, wrapping your brain around closures and asynchronous code will be the two big challenges. Closures are definitely weird concept at first if you have no functional programming background, but once you get used to the idea of functions as just another type of data, the concept isn't so hard to grasp.&nbsp;&nbsp;