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 .
×

Adding inventory weight

1587571192

Edited 1587573637
Is there an API that could add together attributes from different objects in a html fieldset, like a repeating_gear class? And then save them in some different attribute maybe. I want to sum all the weight of a players items in their sheet inventory. Every item is a different gearitem in the repeating_gear class. Is there an API or script that could do it? I havent used the API alot, just some tinkering with chatsetattr.
1587576312
GiGs
Pro
Sheet Author
API Scripter
Is this meant to be a separate script, or a sheet worker on a custom sheet? If it's a sheet worker on character sheet, repeatingSum  does exactly what youre asking for. If it's a separate script, that might have to be a custom design. I dont know if there are any existing scripts that do that (but there might be).
sheet worker should work fine, thanks ill have a look at it
1587576923
GiGs
Pro
Sheet Author
API Scripter
In that case, you just need to copy the first script on that page (dont edit it), and then add a sheet worker, like so: /* ===== PARAMETERS ========== destination = the name of the attribute that stores the total quantity section = name of repeating fieldset, without the repeating_ fields = the name of the attribute field to be summed can be a single attribute: 'weight' or an array of attributes: ['weight','number','equipped'] multiplier (optional) = a multiplier to the entire fieldset total. For instance, if summing coins of weight 0.02, might want to multiply the final total by 0.02. */ const repeatingSum = (destination, section, fields, multiplier = 1 ) = > { if ( ! Array . isArray (fields)) fields = [fields]; getSectionIDs (`repeating_${section}`, idArray = > { const attrArray = idArray. reduce ( (m,id) = > [...m, ...(fields. map (field = > `repeating_${section}_${id}_${field}`))],[]); getAttrs (attrArray, v = > { console. log ( "===== values of v: " + JSON. stringify (v) + " = = = = = "); // getValue: if not a number, returns 1 if it is 'on' (checkbox), otherwise returns 0.. const getValue = (section, id,field) = > parseFloat (v[`repeating_${section}_${id}_${field}`]) | | (v[`repeating_${section}_${id}_${field}`] = = = 'on'  ? 1  : 0 ); const sumTotal = idArray. reduce ((total, id) = > total + fields. reduce ((subtotal,field) = > subtotal * getValue (section, id,field), 1 ), 0 ); setAttrs ({[destination]: sumTotal * multiplier}); }); }); }; on ( 'change:repeating_gear remove:repeating_gear' , function () { repeatingSum ( " encumbrance_total " ,"gear","gearitem"); }); and replace encumbrance_total with the name of the attribute where you want the total to be saved.
1587578763

Edited 1587580023
hmm it only updates the total_weight to 0 everytime a gearitem is added or removed. not sure if ive put it in correctly
1587580000
GiGs
Pro
Sheet Author
API Scripter
It's posisble to use whatever attributes are relevant. You just need to change the stats in this line: repeatingSum ( " encumbrance_total " ,"gear","gearitem"); The first one (encumbrance_total) is the name of the attribute you want to output the total to. It should be outside the repeating section. The second one, "gear" is the repeating section name, without the repeating_ part. The final one is the name of the attribute in the repeating section you want to add up. If that is weight_total, you'd put that there. Also you can put an array of names there. Imagine this: repeatingSum ( " encumbrance_total " , "gear", ["itemweight", itemcount"]); Imagine your repeating section had an itemweight attribute, which held the weight of individual items, and itemcount, which held how many of that item you had. The above will correctly multiple them together for each row of the encumbrance section.
should i have the repeatingSum function and the on('change:') worker all under <script type="text/worker">?
1587580369
GiGs
Pro
Sheet Author
API Scripter
Yes. You should only have one script section, and all sheet workers and functions like the above go inside that.
ok atm i have it as: <script type="text/worker"> /* ===== PARAMETERS ========== destination = the name of the attribute that stores the total quantity section = name of repeating fieldset, without the repeating_ fields = the name of the attribute field to be summed       can be a single attribute: 'weight'       or an array of attributes: ['weight','number','equipped'] multiplier (optional) = a multiplier to the entire fieldset total. For instance, if summing coins of weight 0.02, might want to multiply the final total by 0.02. */ const repeatingSum = (destination, section, fields, multiplier = 1) => {     if (!Array.isArray(fields)) fields = [fields];     getSectionIDs(`repeating_${section}`, idArray => {         const attrArray = idArray.reduce( (m,id) => [...m, ...(fields.map(field => `repeating_${section}_${id}_${field}`))],[]);         getAttrs(attrArray, v => {             console.log("===== values of v: "+ JSON.stringify(v) +" =====");                  // getValue: if not a number, returns 1 if it is 'on' (checkbox), otherwise returns 0..             const getValue = (section, id,field) => parseFloat(v[`repeating_${section}_${id}_${field}`]) || (v[`repeating_${section}_${id}_${field}`] === 'on' ? 1 : 0);              const sumTotal = idArray.reduce((total, id) => total + fields.reduce((subtotal,field) => subtotal * getValue(section, id,field),1),0);             setAttrs({[destination]: sumTotal * multiplier});             });     }); }; on('change:repeating_gear remove:repeating_gear', function() { repeatingSum("encumbrance_gear_weight","gear","true_weight"); }); </script>
however it only sums encumbrance_gear_weight to 0 when gear is changed
1587580553

Edited 1587580570
if there isnt anything wrong in this part i have to have mucked it up with the attributes i guess
1587580651
GiGs
Pro
Sheet Author
API Scripter
Can you post the code for your repeating_gear section?
&lt;div class="gear"&gt; &lt;!-- GEAR --&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;div class="myrow right inventory"&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;div class="blacklabel right title"&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;img class="brdleft" src="<a href="https://raw.githubusercontent.com/Roll20/roll20-character-sheets/master/Pathfinder%20by%20Roll20/img/brdleft.png" rel="nofollow">https://raw.githubusercontent.com/Roll20/roll20-character-sheets/master/Pathfinder%20by%20Roll20/img/brdleft.png</a>" /&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;span class="big only gear"&gt;Gear&lt;/span&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;span class="med only qty" title="Quantity"&gt;Qty&lt;/span&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;span class="med only weight" &gt;Weight&lt;/span&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;span class="med only tot" &gt;Tot&lt;/span&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/div&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/div&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;div class="bordered titled inboxright"&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;fieldset class="repeating_gear"&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input class="options-flag" name="attr_options-flag" type="checkbox" data-i18n-title="show-hide-notes" title="Show/hide notes" /&gt;&lt;span&gt;y&lt;/span&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;div class="myrow right"&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input type="hidden" name="attr_fromcompendium" class="fromcompendium" value="" /&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;button class="myrol" type="compendium" name="attr_fromcompendium" data-i18n-title="open-compendium-item" title="Open Compendium Item"&gt;&lt;/button&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input type="text" name="attr_name" class="underlined itemname" spellcheck="false" title="@{repeating_gear_$id_name}" /&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input type="number" name="attr_quantity" step="0.01" min="1" value="1" class="nospin underlined sml" spellcheck="false" title="@{repeating_gear_$id_quantity}" /&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input type="number" name="attr_weight" step="0.01" min="1" value="1" class="nospin underlined sml margindent" spellcheck="false" title="@{repeating_gear_$id_weight}" /&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input type="hidden" name="attr_weight_total" value="1" /&gt; =&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input type="number" placeholder="tot" value="round(@{weight}*@{quantity})" name="attr_weight_total" disabled="true"&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/div&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input type="hidden" title="total" value="@{weight_total}" name="attr_true_weight" disabled="true"&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;div class="options" style="text-align: left;"&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;div class="myrow"&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;span class="lbl std right" &gt;Cost:&lt;/span&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input type="text" name="attr_cost" value="" spellcheck="false" class="underlined" title="@{repeating_gear_$id_cost}" /&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/div&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;div class="myrow"&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;span class="lbl std right" &gt;Slot:&lt;/span&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input type="text" name="attr_slot" value="" spellcheck="false" class="underlined" title="@{repeating_gear_$id_slot}" /&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/div&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;div class="myrow"&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;span class="lbl std right" &gt;Special:&lt;/span&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;div class="autoex std"&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;span name="attr_special"&gt;&lt;/span&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;textarea name="attr_special" class="underlined" spellcheck="false" title="@{repeating_gear_$id_special}"&gt;&lt;/textarea&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/div&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/div&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;div class="myrow"&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;span class="lbl std right" &gt;Description:&lt;/span&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;div class="autoex std"&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;span name="attr_notes"&gt;&lt;/span&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;textarea name="attr_notes" class="underlined" spellcheck="false" data-i18n-placeholder="notes" placeholder="Notes" title="@{repeating_gear_$id_notes}"&gt;&lt;/textarea&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/div&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/div&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/div&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/fieldset&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;div class="myrow right gearitem"&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;span class="lab"&gt;Weight of coins&lt;/span&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input type="number" name="attr_encumbrance_coins_weight" disabled="true" value="sum(0.00311*@{money_cp}+0.026*@{money_sp}+0.033*@{money_gp}+0.030*@{money_pp})"&gt;&lt;/span&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/div&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;div class="blacklabel total right"&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;span class="big" &gt;Total Weight&lt;/span&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input type="hidden" name="attr_encumbrance" value="light" class="overload" /&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;span class="lbl margindent overload" tabindex="0" name="attr_encumbrance_display"&nbsp; title="@{encumbrance_display}"&gt;&lt;/span&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input name="attr_encumbrance_gear_weight" type="number" class="pseudoattr right greybg wtotal margindent" title="@{encumbrance_gear_weight}"&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/div&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/div&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/div&gt; &lt;!-- END GEAR --&gt;
its a modified pathfinder sheet from roll20
1587581538
GiGs
Pro
Sheet Author
API Scripter
The problem is you weight_total attribute as an autocalc field, and sheet workers dont work with them. Change the sheet worker to bypass it like so on('change:repeating_gear remove:repeating_gear', function() { repeatingSum("encumbrance_gear_weight","gear", ["weight", "quantity"]); }); I would recommend changing your names in this section from 'name', 'weight', 'quantity', etc, to 'gear_name', 'gear_weight', 'gear_quantity' (and so alter the above worker too). This is because roll20 recommends you do not have the same attribute names inside a repeating section and outside, and you might have another weight, name, etc somewhere else on the sheet. You have to make sure your names are unique. A final observation: you have a weight_total&nbsp; that is visible, another which is hidden, and a true_weight that is just a copy of weight_total. This seems to be a lot of unnecessary duplication: you only need one of these.
OMG it works! thank you ! :D&nbsp; Yeah my naming is a bit sloppy, still a nooby&nbsp;
the true_wieght was me trying to find out was what going wrong with the script, will be removed now that you've solved it, thanks again :)
1587581904
GiGs
Pro
Sheet Author
API Scripter
You're welcome! :)