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

Populating a field by table reference, where the input value is the sum of two other values.

1586675749

Edited 1586676111
I have a function: <script type="text/worker"> const int = score => parseInt(score) || 0; on('change:physical_strength', () => { getAttrs(['physical_strength'], values => { const physical_strength = int(values.physical_strength); console.log(physical_strength); const melee_damage_adjustment_table = [-4, -3, -2, -1, -1, -1, -1, -1, 0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19]; const melee_damage_adjustment = melee_damage_adjustment_table[((physical_strength <100 ) ? Math.ceil(physical_strength/5) : Math.ceil(physical_strength / 10) +10) -1]; setAttrs({ melee_damage_adjustment: melee_damage_adjustment }); }); }); </script> It populates a field by table reference. This is the field that stores the input value:     <div class="pss blueBackground section">         <input class="field ability" name="attr_physical_strength" type="number" value='0' min='1' max='220'>     </div> This is the field where the output value is stored:     <div class="mg blueBackground section">         <input name="attr_melee_damage_adjustment" type="hidden" value="0">         <span class="field derived" name="attr_melee_damage_adjustment"></span>     </div> The function works just fine.  The problem is that I want it to do more.  Specifically, I want to split the input value up into two different values which are added up to form the input value. This is the first value to be added:     <div class="pss blueBackground section">         <input class="field ability" name="attr_physical_strength" type="number" value='0' min='1' max='220'>     </div> You'll notice this is the same value I showed you earlier. This is the second value to be added:     <div class="tps blueBackground section">         <input class="field manual" name="attr_temp_physical_strength" type="number" value='0'>     </div> It's used to store temporary adjustments like ability damage. This is the field that stores the sum of the two values I just mentioned:     <div class="pst blueBackground section">         <input class="field derivedAbility" name="attr_physical_strength_total" value="@{physical_strength}+@{temp_physical_strength}" disabled="true"/>     </div> I would like to use this sum value as the input value for the function I showed you earlier. The problem is that the function breaks when I split the input value up. I have attempted to solve this problem already, but I'm not going to show you that code, because I don't think it would be helpful, and it could be confusing.  I believe the problems I'm having are: Making sure that melee_damage_adjustment is recalculated whenever any of these values change:  attr_physical_strength attr_temp_physical_strength attr_physical_strength_total The fact I'm not sure what order melee_damage_adjustment and attr_physical_strength_total are calculated and stored in. Thank you for reading.
1586684501

Edited 1586684576
GiGs
Pro
Sheet Author
API Scripter
One problem is, sheet workers don't work with disabled inputs. They cant set their value, and they cant read from their value accurately. Sheetworkers see the text "@{physical_strength}+@{temp_physical_strength}", they dont see the number they add up to.  So any attribute that is to be used in a sheet worker cannot involve autocalc fields, and must itself be set with sheet workers. In this case, that's simple though. You just combine the addition and setting of strength total with the bonus calculation. Change the bit inside the script lines to this: const int = score => parseInt(score) || 0; on('change:physical_strength change:temp_physical_strength', () => {     getAttrs(['physical_strength', 'temp_physical_strength'], values => {         const strength_base = int(values.physical_strength);         const strength_temp = int(values.temp_physical_strength);         const strength_total = strength_base + strength_temp;         console.log(strength_total);         const melee_damage_adjustment_table = [-4, -3, -2, -1, -1, -1, -1, -1, 0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19];         const melee_damage_adjustment = melee_damage_adjustment_table[((strength_total <100 ) ? Math.ceil(strength_total/5) : Math.ceil(strength_total / 10) +10) -1];         setAttrs({                  physical_strength_total: strength_total,                 melee_damage_adjustment: melee_damage_adjustment         });     }); }); Remember to change your strength total input to this <input class="field derivedAbility" name="attr_physical_strength_total" value="" readonly /> and it will be set by the sheet worker whenever the two stats its based on change, and the bonus will be calculated at the same time.
Thanks for your help GiGs. This does work for me.  Unfortunately, melee_damage_adjustment is only one of four fields that are populated by workers using physical_strength as the input.  The other three need to be moved to physical_strength_total as well.  That means they all will need to be incorporated into this worker.  In addition, physical_strength is actually a subtotal of a half dozen or so other values, like dice rolls and racial modifiers.  It's a lot. On the positive side though, I did break and then fix your code without realizing I was the one who broke it.  I also can use console.log now.  So I am learning.  Wish me luck.
1586851937
GiGs
Pro
Sheet Author
API Scripter
Congratulations :) Are all 4 workers based just on strength changes, with no other stats involved? If so, its best to put them them all in one worker.
Thank you = ).  Yes, they are all just based on strength changes.  No other stats are involved.  I think it's essential to put them all in one worker, and here is that worker: <div class="pss blueBackground section"> <input class="field ability" name="attr_physical_strength" type="number" value='0' min='1' max='220'> <div class="fieldLabel white section">Physical Strength</div> </div> <div class="tps blueBackground section"> <div class="fieldLabel white section">Tmp Adj</div> <input class="field manual23 right" name="attr_temp_physical_strength" type="number" value='0'> </div> <div class="pst blueBackground section"> <input class="field derivedAbility38 section" name="attr_physical_strength_total" value="" readonly /> </div> <div class="mg blueBackground section"> <div class="fieldLabel white section">Melee Dmg</div> <input name="attr_melee_damage_adjustment" type="hidden" value="0"> <span class="field derived" name="attr_melee_damage_adjustment"></span> </div> <div class="en blueBackground section"> <div class="fieldLabel white section">Max Encumbrance</div> <input name="attr_maximum_encumbrance" type="hidden" value="0"> <span class="field derived" name="attr_maximum_encumbrance"></span> </div> <div class="mw blueBackground section"> <div class="fieldLabel white section">Max Weight</div> <input class="attr_maximum_weight" type="hidden" value="0"> <span class="field derived" name="attr_maximum_weight"></span> </div> <div class="psl blueBackground section"> <div class="fieldLabel white section">Skill Limit</div> <input name="attr_physical_strength_skill_limit" type="hidden" value="0"> <span class="field derived" name="attr_physical_strength_skill_limit"></span> </div> <script type="text/worker"> const int = score => parseInt(score) || 0; on('change:physical_strength change:temp_physical_strength', () => { getAttrs(['physical_strength', 'temp_physical_strength'], values => { const strength_temp = int(values.temp_physical_strength); const strength_base = int(values.physical_strength); const strength_total = strength_base + strength_temp; const lookup_value = ((strength_total <100) ? Math.ceil(strength_total/5) : Math.ceil(strength_total/10) +10) -1 const dmg_enc_wgt_table = [             /* 1-5 */ [-4, "2 pounds", "30 pounds", "1 skill"],             /* 6-10 */ [-3, "5 pounds", "40 pounds", "2 skills"],             /* 11-15 */ [-2, "10 pounds", "50 pounds", "3 skills"],             /* 16-20 */ [-1, "20 pounds", "60 pounds", "4 skills"],             /* 21-25 */ [-1, "30 pounds", "70 pounds", "5 skills"],             /* 26-30 */ [-1, "35 pounds", "80 pounds", "5 skills"],             /* 31-35 */ [-1, "35 pounds", "80 pounds", "5 skills"],             /* 36-40 */ [-1, "40 pounds", "90 pounds", "6 skills"],             /* 41-45 */ [+0, "45 pounds", "115 pounds", "6 skills"],             /* 46-50 */ [+0, "60 pounds", "140 pounds", "7 skills"],             /* 51-55 */ [+0, "70 pounds", "170 pounds", "7 skills"],             /* 56-60 */ [+1, "95 pounds", "220 pounds", "8 skills"],             /* 61-65 */ [+1, "120 pounds", "255 pounds", "8 skills"],             /* 66-70 */ [+2, "130 pounds", "280 pounds", "9 skills"],             /* 71-75 */ [+3, "150 pounds", "305 pounds", "9 skills"],             /* 76-80 */ [+3, "165 pounds", "330 pounds", "10 skills"],             /* 81-85 */ [+3, "165 pounds", "330 pounds", "10 skills"],             /* 86-90 */ [+4, "180 pounds", "380 pounds", "10 skills"],             /* 91-95 */ [+5, "200 pounds", "480 pounds", "11 skills"],             /* 96-100 */ [+6, "300 pounds", "640 pounds", "11 skills"],             /* 101-110 */       [+7, "350 pounds", "700 pounds", "12 skills"],             /* 111-120 */       [+8, "390 pounds", "810 pounds", "12 skills"],             /* 121-130 */       [+9, "450 pounds", "970 pounds", "13 skills"],             /* 131-140 */       [+10, "550 pounds", "1130 pounds", "13 skills"],             /* 141-150 */       [+11, "625 pounds", "1440 pounds", "14 skills"],             /* 151-160 */       [+12, "800 pounds", "1750 pounds", "14 skills"],             /* 161-170 */       [+14, "950 pounds", "2000 pounds", "15 skills"],             /* 171-180 */       [+15, "1050 pounds", "2250 pounds", "15 skills"],             /* 181-190 */       [+16, "1100 pounds", "2500 pounds", "15 skills"],             /* 191-200 */       [+17, "1250 pounds", "2750 pounds", "15 skills"],             /* 201-210 */       [+18, "1600 pounds", "3000 pounds", "15 skills"],             /* 211-220 */       [+19, "1750 pounds", "3250 pounds", "15 skills"]     ]; const melee_damage_adj = dmg_enc_wgt_table[lookup_value][0]; const max_encumbrance = dmg_enc_wgt_table[lookup_value][1]; const max_weight = dmg_enc_wgt_table[lookup_value][2]; const str_skill_limit = dmg_enc_wgt_table[lookup_value][3]; console.log(strength_total); console.log(lookup_value); console.log(melee_damage_adj); console.log(max_encumbrance); console.log(max_weight); console.log(str_skill_limit); setAttrs({ physical_strength_total: strength_total, melee_damage_adjustment: melee_damage_adj, maximum_encumbrance: max_encumbrance, maximum_weight: max_weight, physical_strength_skill_limit: str_skill_limit }); }); }); </script> I love multidimensional arrays = ).
1586854694
GiGs
Pro
Sheet Author
API Scripter
Nice work. when you're making multiple console.log statements, its handy to be able to include a label, which you can do like this console.log('strength total: ' + strength_total); or console.log(`strength total: ${strength_total}`); You can also combine them into the same statement console.log('strength total: ' + strength_total + '; Lookup Value: ' + lookup_value); console.log(`strength total: ${strength_total}; Lookup Value: ${lookup_value}`);