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

Simple sheet worker issue? (hopefully)

Hello, So im having a little trouble with the sheetworkers.  With my current code, I cant seem to get it to even trigger in the console. on("change:int change:wiz sheet:opened change:PBvalue", function() {     getAttrs(["intsmod","wismod"], function(values) {     const ints = parseInt(values["intsmod"], 10)||0;     const wis = parseInt(values["wismod"], 10)||0;     var profb = ints + wis; var profb1 = 0; if (profb <=0 ){ profb1 = 0;} else{ profb1 = (ints+wis)/2; }     setAttrs({                                   "PBvalue": Math.floor(profb1)     });   }); }); What im trying to do is set it up so if the values of ints+wis are lesser or equal to zero PBvalue will be set to zero, otherwise its going to be (ints+wis)/2 value rounded down. intsmod and wismod are autocalculated fields. Could I get some help please?
1619269174
Andreas J.
Forum Champion
Sheet Author
Translator
there might be an issue due to using auto-claculated values, try add cosole.log() to see if u ever get a value for them.
1619275363
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Yep, it's definitely a problem with using autocalculated fields. When a sheetWorker gets the value of an autocalculated value, what it sees is the formula of the autocalc (e.g. (@{strength}-10)/2). You can create a recursive replacer that will go through and replace all of those values, but it requires a large change to how you approach sheetworkers, and in general autocalculations should not be mixed with sheetworkers.
1619289576

Edited 1619291162
ok, so I got rid of the autocalc function. Now im trying to replace one of the autocalced function with the sheetworker, however I keep getting  ERROR parsing formula: undefined app.js?1619021958:611 TypeError: Cannot read property 'replace' of undefined     at T.i.autoCalcFormula (app.js?1619021958:603)     at app.js?1619021958:611     at Function.C.each.C.forEach (base.js?1619021697:6)     at T.i.baseUpdateSheetValues (app.js?1619021958:611)     at r (base.js?1619021697:6) in the console. The code is <script type="text/worker"> on("change:characterlevel change:SPUsed sheet:opened", function() {     getAttrs(["characterlevel","SPUsed"], function(values) {     const chl = parseInt(values["characterlevel"])||0;     const spu = parseInt(values["SPUsed"])||0;     var SPT = 0; var spl = 0; if (chl<=0){ SPT = 0;} else{ SPT = 6+((chl-1)*5)} spl = SPT - spu;     setAttrs({                                   "SPTotal": SPT     }); setAttrs({                                   "SPLeft": spl     });   }); }); </script> and as far as I can tell there isnt anything wrong with it.  Uh, could I get some help/tips? Thanks!
Ok, so I tossed a couple of console.logs in there, apparently im doing the setAttrs wrong? Im  not sure how, as the names of the fields are attr_SPTotal, and attr_SPLeft in the html? Do they have to be text type not number?
Solved, had to replace the disabled in the html to readonly. Thanks to everyone who helped :)
1619414766
vÍnce
Pro
Sheet Author
Just mentioning; not sure if it's causing any issues for you, but you might change your event listeners ie on(change:foo) to all lowercase. noted here: <a href="https://wiki.roll20.net/Sheet_Worker_Scripts#Sheet_Worker_API" rel="nofollow">https://wiki.roll20.net/Sheet_Worker_Scripts#Sheet_Worker_API</a> &nbsp; (although I've noticed the very first example contradicts the warning...)