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] If postive set to zero

I'm trying to implement a check with a Sheet Worker for when a value is positve and if then the value is set to 0. But it doesn't want to work and I can't find out why. Anyone here who could help? on("change:kampfstil-prem-stil change:kampf_im_wasser change:sf_kampfimwasser", function() { getAttrs(["kampf_im_wasser_waffe_at"], function(values) { if (values.kampf_im_wasser_waffe_at > "0") { setAttrs({ kampf_im_wasser_waffe_at: 0 }); } }); }); <input name="attr_kampf_im_wasser_waffe_at" type="number" value="(-@{kampf_im_wasser}*2) + (2*@{sf_kampfimwasser} + (4*@{kampfstil-prem-stil}))" disabled="true"/>
1531266610

Edited 1531266632
Victor B.
Pro
Sheet Author
API Scripter
Change "0" to 0 values.kampf_im_wasser_waffe_at > 0
Still doesn't work. When the value gets positive it's not changed to zero.
1531267695

Edited 1531268090
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
I'd further recommend ensuring that your variable is converted to a number first: (values.kampf_im_wasser_waffe_at*1||0) > 0 Note, that the 0 after the "||" should be swapped out for whatever the default value of the variable is. EDIT: And actually, I'd recommend doing this: on("change:kampfstil-prem-stil change:kampf_im_wasser change:sf_kampfimwasser", function() {    getAttrs(["kampf_im_wasser_waffe_at"], function(values) {         setAttrs({             kampf_im_wasser_waffe_at: _.min([values.kampf_im_wasser_waffe_at,0]);         });    }); });
1531267942

Edited 1531268394
GiGs
Pro
Sheet Author
API Scripter
You cant combine an autocalc field and a sheet worker like that. If you are using a sheetworker for part of the calculation, use it for all of it. use the following sheet worker, but for this to work you need to change the attribute named " kampfstil-prem-stil" to " kampfstil_prem_stil" It's good practice, if you're going to use an attribute in a sheet worker, to avoid dashes in the name. Use underscores instead. (note: I edited the code below, i'd missed out something on the getattrs line) on("change:kampfstil_prem_stil change:kampf_im_wasser change:sf_kampfimwasser", function() { getAttrs(["kampfstil_prem_stil","kampf_im_wasser","sf_kampfimwasser"], function(values) { let kampfstil_prem_stil = parseInt(values.kampfstil_prem_stil)||0; let kampf_im_wasser = parseInt(values.kampf_im_wasser)||0; let sf_kampfimwasser = parseInt(values.sf_kampfimwasser)||0; let result = (-@{kampf_im_wasser}*2) + (2*@{sf_kampfimwasser} + (4*@{kampfstil-prem-stil})); if(result > 0) result = 0; setAttrs({ kampf_im_wasser_waffe_at: result }); }); }); Also, change the html to <input name="attr_kampf_im_wasser_waffe_at" type="number" value="0" readonly/> A sheetworker cant modify an autocalc field, or an input set to disabled.
1531268228
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Ah, I missed that issue. Good catch GG. And yeah autocalc is, at least in my opinion, only still around as a legacy feature to keep older sheets supported. It shouldn't be used for new sheets as sheetworkers allow much more flexibility and power.
G G said: You cant combine an autocalc field and a sheet worker like that. If you are using a sheetworker for part of the calculation, use it for all of it. use the following sheet worker, but for this to work you need to change the attribute named " kampfstil-prem-stil" to " kampfstil_prem_stil" It's good practice, if you're going to use an attribute in a sheet worker, to avoid dashes in the name. Use underscores instead. (note: I edited the code below, i'd missed out something on the getattrs line) on("change:kampfstil_prem_stil change:kampf_im_wasser change:sf_kampfimwasser", function() { getAttrs(["kampfstil_prem_stil","kampf_im_wasser","sf_kampfimwasser"], function(values) { let kampfstil_prem_stil = parseInt(values.kampfstil_prem_stil)||0; let kampf_im_wasser = parseInt(values.kampf_im_wasser)||0; let sf_kampfimwasser = parseInt(values.sf_kampfimwasser)||0; let result = (-@{kampf_im_wasser}*2) + (2*@{sf_kampfimwasser} + (4*@{kampfstil-prem-stil})); if(result > 0) result = 0; setAttrs({ kampf_im_wasser_waffe_at: result }); }); }); Also, change the html to <input name="attr_kampf_im_wasser_waffe_at" type="number" value="0" readonly/> A sheetworker cant modify an autocalc field, or an input set to disabled. Works great, thanks!
1531276594
Victor B.
Pro
Sheet Author
API Scripter
Someone want to explain what a sheet worker is?  
1531277702

Edited 1531277753
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
A sheetworker is just the Roll20 term for the javascript that drives Character sheets. We use sheetworker after the set of functions that are available to us to interact indirectly with the DOM (e.g. getAttrs, setAttrs, ...). It's really just normal javascript for any calculation that you do within the script, it's only how you get information from the DOM (i.e. attributes) and how you set things in the DOM (i.e. attributes) that is unique to sheetworkers. More information in the wiki
1531278373
Victor B.
Pro
Sheet Author
API Scripter
Well, I've learned more than a few things in one day.  The wiki helps, thanks.