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

[Help] Basics of Sheet Worker Scripts

1454934068

Edited 1454934579
Hello, I started to play with custom sheets. HTML/CSS wiki was good and helped me to make sheet I wanted. But then I got to Sheet Worked Scripts and in this case the Wiki did not told me much. I tried to make simple worker based on that and it does not work, can someone help? I would like to have following: str and dex as number which can be raised and lowered (that seems to work) acrobatics to be counted automatically as str + dex . (does not work) What am I doing wrong? <div class="sheet-col">     <label>Strength:</label><input type="number" name="attr_str" value="0" /><br/>     <label>Dexterity:</label><input type="number" name="attr_dex" value="0" /><br/>                  <hr/>     <label>Acrobatics:</label>   <input type="number" name='attr_acrobatics' disabled="true" /> </div> <script type="text/worker">          on("change:str change:dex", function() {         getAttrs(["dex", "str"], function(values) {             setAttrs({               acrobatics: values.dex + values.str             });         });     });          </script> Thanks in advance!
1454937534
Kryx
Pro
Sheet Author
API Scripter
disabled="true" prevents it from being set. Use readonly. <input type="number" name="attr_acrobatics" readonly>
Thank you Kryx! .That was part of the problem. I also needed to change strings to int afterwards to change contcatenation to intended sum. acrobatics: parseInt(values.str) + parseInt(values.dex) Is this good way or is there some better?
1454938667
The Aaron
Pro
API Scripter
That's the best way, unless you plan to have decimals.
1454941014
Kryx
Pro
Sheet Author
API Scripter
parseInt should have the radial. I use functions to give me ints and floats from the sheet: function getIntValue(value, defaultValue) { if (!defaultValue) { defaultValue = 0; } return parseInt(value, 10) || defaultValue; } function getFloatValue(value, defaultValue) { if (!defaultValue) { defaultValue = 0; } return parseFloat(value) || defaultValue; }
1454942128
Lithl
Pro
Sheet Author
API Scripter
Kryx said: parseInt should have the radial The radix is an optional argument for parseInt. If no radix is supplied, base-10 is assumed.
1454942589
Kryx
Pro
Sheet Author
API Scripter
Brian said: The radix is an optional argument for parseInt. If no radix is supplied, base-10 is assumed. Entirely true, but I jsHint my code to test for errors or remedial issues and jshint does care. :)
Kryx: thanks. I had to google what is radix but now I get it and agree that it is good to use to prevent unwanted errors :) The Aaron: good point. All final numbers shoud be integers without decimals, but many derived attributes are counted as halves rouded up or down (case by case) - will this work properly when the rounding is done immediately?
1454948316
Lithl
Pro
Sheet Author
API Scripter
Sniper said: Kryx: thanks. I had to google what is radix but now I get it and agree that it is good to use to prevent unwanted errors :) If you're dealing with base-10, omitting the radix on a parseInt call will not cause errors, period. If the input isn't a base-10 number the return value is going to be NaN, but that would be the case even if you included the radix.
1454970182
The Aaron
Pro
API Scripter
  Sniper said: The Aaron: good point. All final numbers shoud be integers without decimals, but many derived attributes are counted as halves rouded up or down (case by case) - will this work properly when the rounding is done immediately? You can use parseFloat() on anything you want to have as a number that might have a decimal.  When you're ready to assign it to an attribute, you can express it in a fixed format with .toFixed(# of places), or use Math.round() to get a rounded integer. attribute: (parseFloat(values.str) + parseFloat(values.dex)).toFixed(2) Note that the return of .toFixed() is a string, so "12.40", not 12.4.  Also, parseFloat() has no radix parameter.
1454970452
Kryx
Pro
Sheet Author
API Scripter
For rounding to 2 I'd suggest Math.round(NUMBER * 100) / 100; It gets rid of some floating point errors. See&nbsp;<a href="http://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-in-javascript" rel="nofollow">http://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-in-javascript</a>