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

Sheet Worker Newbie Help

I'm trying to learn how to integrate some sheet worker scripts in my first character sheet and nothing happens and I can't set anything. I've read every page on the wiki and I'm just reaching out for some help from the community now. The basic script I'm trying to employ is as follows: on("sheet:opened change:fellowship change:unfel", function() {   getAttrs(["fellowship","unfel"], function(values) { const fel = parseInt(values.fellowship,10)||0; const unfel = parseInt(values.unfel,10)||0; const felcalc = Math.floor(fel/2) + unfel; setAttrs({                             felbns: felcalc }); }); }); I've also tried the following based on another post I found on("sheet:opened change:fellowship change:unfel", function() {   getAttrs(["fellowship","unfel"], function(values) { const fel = parseInt(values.fellowship,10)||0; const unfel = parseInt(values.unfel,10)||0; const felcalc = Math.floor(values.fel/2) + values.unfel; setAttrs({                             felbns: values.felcalc }); }); }); Thanks. Also, any other example areas I can read that anyone can recommend that work well.
1579499834
The Aaron
Roll20 Production Team
API Scripter
You can put some console.log() calls one, then look at the JavaScript Console to see what's happening.  Also, be sure you have this in a <script type="text/worker"> block. I always forget and make a "text/javascript". 
1579502100
Andreas J.
Forum Champion
Sheet Author
Translator
The Aaron said: Also, be sure you have this in a <script type="text/worker"> block. I always forget and make a "text/javascript". And I have created a bunch of shorthands that automatically populates the Roll20-specific bells and whistles to my code when using Sublime. :)
1579517230

Edited 1579517243
GiGs
Pro
Sheet Author
API Scripter
The first of your examples looks perfectly fine. The second is not. This section in the second one has the mistake:                 setAttrs({                             felbns: values.felcalc }); The values object is created by getAttrs, and contains the attributes you've declared. So when you do this getAttrs(["fellowship","unfel"], function(values) { this is a command to tell roll20: "look at the character sheet, find the attributes named fellowship and unfel , and put them in a variable called values ." So when this sheet worker runs, it has access to a values object, which is a variable that looks like this: values = {     fellowship: 3,     unfel: 2 } (numbers made up for the example; this is an object  - a type of variable which can hold multiple other variables within it. Here it contains two - fellowship and unfel - their names and their values.) Then when you do this const fel = values.fellowship; (I stripped out the part of the code, to focus on this part) you are accessing that values object, and getting the fellowship value from it. The values object contains only the attributes defined in the getAttrs line. So getting back to this                 setAttrs({                             felbns: values.felcalc }); This is looking for an attribute named felcalc in the values object, which doesnt exist. And so this will throw an error. Your first sheet worker code though is perfectly fine. That should work. That suggests the error is somewhere else in the sheet. My first question is the same as Aaron's-  is the script block set up correctly? Secondly, I'd look at the html for these attributes. Are the attribute names defined properly. Its common for first time coders to forget the "attr_" part of attribute names (I still do it from time to time). If there are autocalc values in either of those attributes, that'll cause issues too (because of the way the code is written, it should still run, but the values of the attribute will always be zero). I'd follow Aaron's suggestion and put a console.log statement or two in your code, like so: console.log("%c===== The script block is working","color:red"); on("sheet:opened change:fellowship change:unfel", function() {           console.log("%c===== The change event fired correctly","color:red"); getAttrs(["fellowship","unfel"], function(values) {             console.log("%c===== The getAttrs command worked, values equals:","color:red");             console.log(values);     const fel = parseInt(values.fellowship,10)||0;             console.log("%c===== fel = " + fel,"color:red");     const unfel = parseInt(values.unfel,10)||0;             console.log("%c===== unfel = " + unfel,"color:red");     const felcalc = Math.floor(fel/2) + unfel;             console.log("%c===== felcalc = " + felcalc,"color:red");     setAttrs({                                     felbns: felcalc                 }); }); }); Then open the browser console when you try to change anything, and you should be able to spot whether anything happens. The comnsole statements will be in red text, with "======" at the start, so should be easy to find.  When you first open the character sheet, before doing anything, look for this in the console : "====== The script block is working" If its not there, there's an issue with your script block and none of your code will work.  Assuming thats fine, you should get the console statements when you change things, and you can check at which point it breaks. If any console statement doesnt appear, then thats where it breaks. If they do appear, but have the wrong values, you can check the html to see why they have the wrong values.
1579537164

Edited 1579537204
Thanks the Aaron and GiGs and Andrea. I got the script worker to function but i had to rip out the Aaron's Sheet from the character sheet. Like anything when I first start trying new things I used someone else's sheet as a baseline to help teach myself and they had&nbsp; /* ---- BEGIN: TheAaronSheet.js ---- */ // Github:&nbsp; &nbsp; <a href="https://github.com/shdwjk/TheAaronSheet/blob/master/TheAaronSheet.js" rel="nofollow">https://github.com/shdwjk/TheAaronSheet/blob/master/TheAaronSheet.js</a> // By:&nbsp; &nbsp; &nbsp; &nbsp;The Aaron, Arcane Scriptomancer // Contact:&nbsp; <a href="https://app.roll20.net/users/104025/the-aaron" rel="nofollow">https://app.roll20.net/users/104025/the-aaron</a> var TAS = TAS || (function(){ &nbsp; &nbsp; 'use strict'; &nbsp; &nbsp; var version = '0.2.3', EDIT: There is more but I'm guessing you don't need to see it all. At the bottom. When I ripped that out of the sheet everything kicked in and started working. I'm not sure why - guessing the original author maybe had something setup but this is fine. The sheet still works just fine without it and I can continue to learn script workers.
1579538236
GiGs
Pro
Sheet Author
API Scripter
It could be as a simple as having missed the end line of the script and its closing brackets when you pasted that in. But it doesnt matter, you have it working now. Thats the important thing :)
So if I have 9 attributes for calculating on the sheet is it better to put them all in one Scriptworker or have one for each attribute or does it not matter?
1579540530
GiGs
Pro
Sheet Author
API Scripter
My usual approach when you have a bunch of almost identical sheet workers is a universal sheet worker - a forEach loop that builds all 9 attributes from a template for one.
1579557249
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Rather than rehash my views on this subject, I'm just going to direct you to the thread where I covered the various patterns of setting up sheet workers .