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

Auto-calculating value attribute arrays

I'm trying to get a value to populate on a custom character sheet. In the system we use, a strength of 14 has a mod of 1, 15 had a mod of 2, 16 has a mod of 3, 17 has a mod of 4, and 18 has a mod of 5.  <input type="number" name="attr_Str">Strength <input type="number" value="____" disabled="true" name="attr_StrMod">Strength Mod What are some options that I have to put in the value="___" to get this to auto calculate? If I could get it to do Str-13 but only display values greater than 0, that would be ideal. I don't think HTML allows if statements, otherwise I could make that work fairly easily. Any ideas?
1523769763

Edited 1523769938
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
(@{str}-13) should work based on the progression you outlined there. Edit: Sorry missed your second paragraph somehow (what I get for forum browsing so late). Something like this might work: {0,@{str}-13}kh1 Also, just (@{str}-13) could work using the ability of html inputs to be limited to certain inputs (can't remember the properties needed for this, but you should be able to google it). Also not sure if autocalc fields respect this.
1523771303

Edited 1523771433
GiGs
Pro
Sheet Author
API Scripter
There are in theory two options: using an autocalc field, or a sheetworker. An autocalc field should be simpler, but I can't figure out how to get it to work, so here's the sheetworker method: First, I'd suggest putting your labels (Strength, etc) inside Label tags, so you can style them later. Also there's no good reason to use abbreviations in attribute names - it's better to use attr_Strength in place of attr_Str. Finally, for the sheetworker method, you need to remove the disabled=true tag and add readonly instead: So your html would look something like: <label>Strength</label> <input type="number" name="attr_Strength"> <label>Mod</label> <input type="number" name="attr_Strength_Mod" value="0" readonly> Then create a script block by adding this at the bottom of your html: <script type="text/worker"> </script> ALL scripts you create go inside this block. Do not create multiple script blocks. A script to update the Strength mod would look like this: on("change:strength sheet:opened", function () { // stat names need to be lower case here getAttrs(["Strength"], function(values) { // this step grabs the value of the stat          let base = parseInt(values.Strength)||0; // we store the stat in a variable so we can manipulate it. parseInt makes sure it is recognised as a number and not text.          let mod = 0; //you could change this to mod = "___"; if you want a number output for 0. But you cant use the mod in macros then, so its not a good idea. if (base > 13) mod = base-13; setAttrs({ "Strength_Mod": mod // save the modifier to the Strength_Mod attribute. }); }); }); This will update the strength mod whenever strength changes. You might need to alter the calculation if there's a negative modifier for scores below 10, or if there's a maximum modifier. The script is easily tweaked. Lets say your system also give a -1 per stat below 8, the script would be tweaked to: let mod = 0; if (base > 13) mod = base-13; else if (base < 8) mod = base-8; Here's an alternate form of the script that will handle any number of attributes. Just change the names on the first line to whatever your attribute list is, and make sure the spelling matches perfectly. ['Strength', 'Dexterity', 'Endurance', 'Intelligence', 'Wisdom', 'Charisma'].forEach(function (name) { on("change:" + name.toLowerCase() + " sheet:opened", function () { getAttrs([name], function(values) {          let base = parseInt(values[name])||0;          let mod = 0; if (base > 13) mod = base-13; setAttrs({ [name + "_Mod"]: mod // save the modifier to the Strength_Mod attribute. }); }); }); });
Thanks guys! I'll give it a shot and see how it works out.