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

Which is better to use Auto-calc or Sheetworker?

1512395731
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
I am working on my combat modifiers section of my sheet. I have a bunch of check boxes and drop downs. All of these need to be totaled and the modifier displayed for the user. Which is better to use Auto-calc or Sheetworker?
1512399737
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Sheetworkers are faster than autocalc, with many autocalc, your sheet will freeze when first opened as everything gets processed. Of course sheetworkers do require some programming and debugging.
1512400034
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
Ah I see
1512401139
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
ok to do a simple sum...because I am new at this. A little help?
1512401846

Edited 1512402074
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
So this is what I came up with // Ranged Combat Modifiers Calculations on('change:r-partialvision change:r-visibility change:tgtsize change:tgtrange change:tgtspeed change:lightcover change:tgtpart change:tgtpost change:rangedtgtlocation change:tgtbehind change:rangedmaneuver change:irritate-cond change:incap-cond change:closecbt change:damweapon change:r-shock change:rangedbadfoot change:rangedminordistract change:rangedmajordistract change:rangedstdeficit change:aim change:xaim change:r-braced change:popupatk change:rapidfire change:r-dualweaponprime change:r-dualweaponoff change:oppfire change:opphexes change:laser change:scope change:v-tgt change:unfamiliar',function (e) { getAttrs(['r-partialvision, r-visibility, tgtsize, tgtrange, tgtspeed, lightcover, tgtpart, tgtpost, rangedtgtlocation, tgtbehind, rangedmaneuver, irritate-cond, incap-cond, closecbt, damweapon, r-shock, rangedbadfoot, rangedminordistract, rangedmajordistract, rangedstdeficit, aim, xaim, r-braced, popupatk, rapidfire, r-dualweaponprime, r-dualweaponoff, oppfire, opphexes, laser, scope, v-tgt, unfamiliar'], function(v) var vals = Object.values(v); vals.push(0); setAttrs({ rangedmod: vals.reduce(getSum) }); });  });
1512402567
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
So, the sheetworkers are javascript. Assuming that these aren't repeating sections, I'd recommend something like this (using example attributes): HTML <input name='attr_size' type='number' value='0'> <input name='attr_range' type='number' value='0'> <input name='attr_speed' type='number' value='0'> <input name='attr_cover' type='number' value='0'> <input name='attr_total' type='number' value='0'> Sheetworker on('change:size change:range change:speed change:cover',(event)=>{ var setObj = {}; getAttrs(['size','range','speed','cover'],(objs)=>{ setObj.total = objs.size*1 + objs.range*1 + objs.speed*1 + objs.cover*1; setAttrs(setObj,{silent:true}); }); });
1512403255
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
I have the idea...the attribute I am trying to set is attr_rangedmod. Is that the Obj?
1512404010
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
So, the objs attribute is just what I have passed the result of getAttrs into the callback function as, it is a javascript object with keys that are the attribute name, and values that are the attribute values. So the objs variable in my example above would look like this if you logged it: { 'size':5, 'speed':30, 'cover':0, 'range':30, }
1512404816
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
Maybe I am not asking the right questions. lets say a series of drop downs and checkboxes, in this instance 'size', 'speed', 'cover', and 'range' sum to a total of -8. How do I get that to display in a readonly input box designated as 'attr_rangedmod'. My old tanker brain is just not making the leap apparently.
1512405411

Edited 1512405504
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Right, so here's my code with some annotations: on('change:size change:range change:speed change:cover',(event)=>{ //If any of the listed attributes changes, run the attached function var setObj = {}; //Create an object to store the attributes to be set getAttrs(['size','range','speed','cover'],(objs)=>{ //get the current values of the listed attributes, and then pass them to the callback function as an object setObj.total = objs.size*1 + objs.range*1 + objs.speed*1 + objs.cover*1;//Create a key in setObj for the attribute to be set, and set it equal to the sum of the relevant attributes. the *1 after each attribute is to make sure that it is converted to a number instead of being a string (what it is if it is the default, or set by the user) setAttrs(setObj,{silent:true}); //Pass setObj to setAttrs to set the attributes contained within setObj and do it silently so that additional sheetworkers aren't triggered. This is equivalent to doing setAttrs({total:X},{silent:true}) }); });
1512405711
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
Alright so what you're say is rangedmod goes var setObj = { here } 
1512406298
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
yep, so instead of setObj.total, you'd do setObj.rangedmod
1512406732
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
Hmm, I still have it broken. // Ranged Combat Modifiers Calculations on('change:r-partialvision change:r-visibility change:tgtsize change:tgtrange change:tgtspeed change:lightcover change:tgtpart change:tgtpost change:rangedtgtlocation change:tgtbehind change:rangedmaneuver change:irritate-cond change:incap-cond change:closecbt change:damweapon change:r-shock change:rangedbadfoot change:rangedminordistract change:rangedmajordistract change:rangedstdeficit change:aim change:xaim change:r-braced change:popupatk change:rapidfire change:r-dualweaponprime change:r-dualweaponoff change:oppfire change:opphexes change:laser change:scope change:v-tgt change:unfamiliar',(event)=>{ var setObj = {rangedmod}; getAttrs(['r-partialvision, r-visibility, tgtsize, tgtrange, tgtspeed, lightcover, tgtpart, tgtpost, rangedtgtlocation, tgtbehind, rangedmaneuver, irritate-cond, incap-cond, closecbt, damweapon, r-shock, rangedbadfoot, rangedminordistract, rangedmajordistract, rangedstdeficit, aim, xaim, r-braced, popupatk, rapidfire, r-dualweaponprime, r-dualweaponoff, oppfire, opphexes, laser, scope, v-tgt, unfamiliar'],(objs)=>{ setObj.rangedmod = objs.r-partialvision*1 + objs.r-visibility*1 + objs.tgtsize*1 + objs.tgtrange*1 + objs.tgtspeed*1 + objs.lightcover*1 + objs.tgtpart*1 + objs.tgtpost*1 + objs.rangedtgtlocation*1 + objs.tgtbehind*1 + objs.rangedmaneuver*1 + objs.irritate-cond*1 + objs.incap-cond*1 + objs.closecbt*1 + objs.damweapon, r-shock*1 + objs.rangedbadfoot*1 + objs.rangedminordistract*1 + objs.rangedmajordistract, rangedstdeficit*1 + objs.aim*1 + objs.xaim, r-braced*1 + objs.popupatk*1 + objs.rapidfire*1 + objs.r-dualweaponprime*1 + objs.r-dualweaponoff*1 + objs.oppfire*1 + objs.opphexes*1 + objs.laser*1 + objs.scope*1 + objs.v-tgt*1 + objs.unfamiliar*1; setAttrs(setObj,{silent:true}); }); });
1512407584
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
So, you're using an attribute syntax that has dashes in it, these represent minuses in javascript, so your objs.r-partialvision*1 is actually parsing as objs.r minus partialvision*1. You can still use these attribute names, you just need to change how you reference them in the javascript code. Change your setObj.rangedmod = line to this: setObj.rangedmod = objs[r-partialvision]*1 + objs[r-visibility]*1 + objs.tgtsize*1 + objs.tgtrange*1 + objs.tgtspeed*1 + objs.lightcover*1 + objs.tgtpart*1 + objs.tgtpost*1 + objs.rangedtgtlocation*1 + objs.tgtbehind*1 + objs.rangedmaneuver*1 + objs[irritate-cond]*1 + objs.incap-cond*1 + objs.closecbt*1 + objs.damweapon, r-shock*1 + objs.rangedbadfoot*1 + objs.rangedminordistract*1 + objs.rangedmajordistract, rangedstdeficit*1 + objs.aim*1 + objs.xaim, r-braced*1 + objs.popupatk*1 + objs.rapidfire*1 + objs.r-dualweaponprime*1 + objs.r-dualweaponoff*1 + objs.oppfire*1 + objs.opphexes*1 + objs.laser*1 + objs.scope*1 + objs.v-tgt*1 + objs.unfamiliar*1;
1512408302
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
I didn't know that. :)
1512408670
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
np, happy rolling.
1512410830

Edited 1512411007
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
I went through and edited the attr's with hyphens. Still no work. Soooo let me see if I can do this on a smaller scale. Changing this:  <input type="number" name="attr_ttlrange" value="@{tgtsize} + @{tgtrange} + @{tgtspeed}" disabled="true" /> into this // Total Speed/Range/Size on('change:tgtsize change:tgtrange change:tgtspeed',(event)=>{ var setObj = {ttlrange}; getAttrs(['tgtsize', 'tgtrange', 'tgtspeed'],(objs=>{ setObjs.total = objs.tgtsize*1 + objs.tgtrange*1 + objs.tgtspeed*1; setAttrs(setObj,{silent:true}); }); });
1512411408
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
oh, were your attributes all still disabled='true'? Those are autocalc fields then, and not editable by the sheetworker. Also, missed this last time, but your var setObj declaration is incorrect. var setObj = {ttlrange} or {rangedmod} isn't valid. Just set it to an empty object (setObj={}), you'll fill it as totals are calculated. Beyond that, do you get any errors in the console log?
1512412015
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
Here is the attr I am trying to display <input type="number" name="attr_speedrangesize" readonly="readonly" value="0" />
1512412077
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
And this is the sheetworker that I want to send the info from on('change:tgtsize change:tgtrange change:tgtspeed',(event)=>{ var setObj = {speedrangesize}; getAttrs(['tgtsize', 'tgtrange', 'tgtspeed'],(objs=>{ setObjs.total = objs.tgtsize*1 + objs.tgtrange*1 + objs.tgtspeed*1; setAttrs(setObj,{silent:true}); }); });
1512412307

Edited 1512412347
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Ok, your setObj declaration is still incorrect. Should be this: on('change:tgtsize change:tgtrange change:tgtspeed',(event)=>{ var setObj = {}; getAttrs(['tgtsize', 'tgtrange', 'tgtspeed'],(objs=>{ setObjs.total = objs.tgtsize*1 + objs.tgtrange*1 + objs.tgtspeed*1; setAttrs(setObj,{silent:true}); }); }); Also, are you getting any errors in your console?
1512412470
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
How do I even look at the console? I have only limited Rol20-Fu
1512412620
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
How does the sheetworker send the calculation to the input "speedrangesize"? I am not seeing it.
1512412798
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
The console is how you inspect code in the browser, right click on an element and then select inspect element. This will bring up the html/css inspection utility of your browser. At the top of that will be options for inspection, the console, and several others: You can also access this via CMD-ALT-I on a MAC (don't know the shortcut for PC)
1512412881

Edited 1512412915
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Scott S. said: How does the sheetworker send the calculation to the input "speedrangesize"? I am not seeing it. Oh, sorry, missed it, but you didn't change setObj.total from my example: on('change:tgtsize change:tgtrange change:tgtspeed',(event)=>{ var setObj = {}; getAttrs(['tgtsize', 'tgtrange', 'tgtspeed'],(objs)=>{ setObj.speedrangesize = objs.tgtsize*1 + objs.tgtrange*1 + objs.tgtspeed*1; setAttrs(setObj,{silent:true}); }); });
1512413445
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
Well there it is. I was scratching a hole in my head trying to figure it out. Dang it, I've been using the console all along....Doh
1512415486
SᵃᵛᵃǤᵉ
Sheet Author
API Scripter
however it still didnt work. This did though. on('change:tgtsize change:tgtrange change:tgtspeed', function (e) { getAttrs(['tgtsize', 'tgtrange', 'tgtspeed'], function(v) { var vals = Object.values(v); vals.push(0); setAttrs({ speedrangesize: vals.reduce(getSum) }); }); });