I made a tweak to finderski's function. This handles quantities and on/off statuses within the repeatingSimpleSum formula.
function repeatingSum (destination, section, fieldsum, fieldmult, totalmult){
// destination = the name of the attribute that stores the total weight
// section = name forof repeating fieldset, without the repeating_
// fieldsum = the name of the "weight" field to be summed
// fieldmult = the name of quantity field, that multiplies weight.
// Leave as "false" if ignored. If using a checkbox rather than a number input, set its value to 1.
// totalmult = a multiplier to ever item in the fieldset. For instance, coinage might be 0.02 each, so multiple the final total by 0.02
// you can leave fieldmult and totalmult off.
var repSection = "repeating_"+section;
let multiplier = true;
if (fieldmult === undefined || fieldmult === false || fieldmult === 'false') multiplier = false;
if (totalmult === undefined) totalmult = 1;
getSectionIDs(repSection, function(idArray) {
//Construct Array of fields to get attributes for...
var sumFields = [];
var multFields = [];
for (var a=0; a< idArray.length; a++) {
sumFields[sumFields.length] = repSection + "_" + idArray[a] + "_" + fieldsum;
if(multiplier) multFields[multFields.length] = repSection + "_" + idArray[a] + "_" + fieldmult;
}
getAttrs(sumFields.concat(multFields), function(v) {
console.log("%%% values of v: "+ JSON.stringify(v) +" %%%");
let sumTotal = 0;
let tempSum = 0;
for(a=0; a<idArray.length; a++){
tempSum = parseInt(v[sumFields[a]]) || 0;
if(multiplier) {
tempSum = tempSum * parseInt(v[multFields[a]]) || 0;
}
sumTotal += tempSum;
console.log("$$$ sumTotal: " + sumTotal + " $$$");
}
var setSumValue = {};
setSumValue[destination]=sumTotal * totalmult;
setAttrs(setSumValue);
});
});
}
Some examples of calling it:
say you have an inventory fieldset, and you might carry, say, 1 sword, but 10 arrows:
itemweight = the input box that contains the weight of a single item,
itemcount = the input box that contains the number of items.
weight_carried = the box that contains the total weight of your gear
on('change:repeating_inventory remove:repeating_inventory sheet_opened', function() {
repeatingSum("weight_carried", "inventory","itemweight","itemcount");
});
Say you have an armour fieldset, and you might have several suits of armour. Each has a checkbox (name: armour_on), with a value =1. When you wear it, check the box, and the armour is calculated.
on('change:repeating_armour remove:repeating_armour sheet_opened', function() {
repeatingSum("weight_worn", "armour","armour_weight","armour_on");
});
Finally say you have a place for special artefacts, and you only ever have one of each. In this case, the multiplier field is set to false and ignored.
on('change:repeating_artifacts remove:repeating_artifacts sheet_opened', function() {
repeatingSum("weight_artifacts","artifacts","artifact_weight",false);
});
You can also skip the last field entirely:
on('change:repeating_artifacts remove:repeating_artifacts sheet_opened', function() {
repeatingSum("weight_artifacts","artifacts","artifact_weight");
});
Then you have total_encumbrance box, and this makes sure they all add correctly
on('change:weight_artifacts change:weight_carried change:weight_worn sheet_opened', function() {
getAttrs(['weight_artifacts','weight_worn','weight_carried'], function(v) {
setAttrs({
encumbrance_total: parseInt(v.weight_artifacts) || 0 + parseInt(v.weight_carried) || 0 + parseInt(v.weight_worn) || 0
})
});
});
Thanks for posting it finderski. I like this approach a lot, and will be replacing the method I used to do it which was a little clunkier.