
Below I have a sheetworker script for calculating either stress or health which are primarily tracked on the sheet with check boxes. I also have an attribute set up for the health and the health_max so that they can be tracked using linked token bar. The script will take a change to the attribute and loop through assigning the proper amount of check boxes toggled to 1. Alternatively if a check box is checked or unchecked it will update the attribute to the new value. <script type="text/worker">
// CALCULATE STRESS - HEALTH - VALUE FROM CHECKBOXES OR ATTRIBUTE
const variableAttributes = ["stress","health"];
variableAttributes.forEach(function (variableAttribute) {
on("change:" + variableAttribute + " change:" + variableAttribute + "_1 change:" + variableAttribute + "_2 change:" + variableAttribute + "_3 change:" + variableAttribute + "_4 change:" + variableAttribute + "_5 change:" + variableAttribute + "_6 change:" + variableAttribute + "_7 change:" + variableAttribute + "_8 change:" + variableAttribute + "_9 change:" + variableAttribute + "_10 change:" + variableAttribute + "_11 change:" + variableAttribute + "_12", function(eventinfo) {
getAttrs([variableAttribute, variableAttribute + "_max", variableAttribute + "_1", variableAttribute + "_2", variableAttribute + "_3", variableAttribute + "_4", variableAttribute + "_5", variableAttribute + "_6", variableAttribute + "_7", variableAttribute + "_8", variableAttribute + "_9", variableAttribute + "_10", variableAttribute + "_11", variableAttribute + "_12" ], function (values) {
console.log("%c|*** Character Sheet Calculating Attribute Value Change ***|", "color:Black; background-color: LawnGreen");
console.log(eventinfo.sourceAttribute + " was activated by " + eventinfo.sourceType);
console.log("Previous Value: " + eventinfo.previousValue + " changed to a New Value of: " + eventinfo.newValue);
console.log(JSON.stringify(eventinfo));
if ( eventinfo.sourceAttribute == variableAttribute ) {
let setAttributeValuesAttrs = {};
for(let a=1; a <= values[variableAttribute + "_max"]; a++) {
if (a <= eventinfo.newValue) {
setAttributeValuesAttrs[variableAttribute+"_"+a] = 1;
console.log("Setting Attribute Values: " + variableAttribute + "_" + a + " = 1");
}
else {
setAttributeValuesAttrs[variableAttribute+"_"+a] = 0;
console.log("Setting Attribute Values: " + variableAttribute + "_" + a + " = 0");
}
setAttrs(setAttributeValuesAttrs, {silent:true});
}
}
else {
let setAttributeFinalAttrs = {};
const attributeTotal = (parseInt(values[variableAttribute + "_1"], 10)||0) + (parseInt(values[variableAttribute + "_2"], 10)||0) + (parseInt(values[variableAttribute + "_3"], 10)||0) + (parseInt(values[variableAttribute + "_4"], 10)||0) + (parseInt(values[variableAttribute + "_5"], 10)||0) + (parseInt(values[variableAttribute + "_6"], 10)||0) + (parseInt(values[variableAttribute + "_7"], 10)||0) + (parseInt(values[variableAttribute + "_8"], 10)||0) + (parseInt(values[variableAttribute + "_9"], 10)||0) + (parseInt(values[variableAttribute + "_10"], 10)||0) + (parseInt(values[variableAttribute + "_11"], 10)||0) + (parseInt(values[variableAttribute + "_12"], 10)||0);
{setAttributeFinalAttrs[variableAttribute] = attributeTotal}
console.log("Setting Attribute Values: " + variableAttribute + " = " + attributeTotal );
setAttrs (setAttributeFinalAttrs, {silent:true})
}
});
});
});
</script> It works very well from the character sheet but if you try to change the value of the linked token bar it will return an undefined newValue / previousValue. See console logs below. Change from 6 to 5 from character sheet to @{health} attribute: |*** Character Sheet Calculating Attribute Value Change ***|
health was activated by player
Previous Value: 6 changed to a New Value of: 5
{"sourceAttribute":"health","sourceType":"player","triggerName":"health","previousValue":"6","newValue":"5"} Change from 5 to 6 from token bar linked to @{health} attribute: |*** Character Sheet Calculating Attribute Value Change ***|
health was activated by player
Previous Value: undefined changed to a New Value of: undefined
{"sourceAttribute":"health","sourceType":"player","triggerName":"health"} Changing the Value in the linked token bar will change the attribute on the character sheet but since I'm using the eventInfo.newValue in the sheetworker calculations it throws a wrench in my code and it clears all of the check boxes tracking health. Is not having the linked token bar give eventInfo,previousValue & eventInfo.newValue a bug? An oversight? Not Possible? Wes *Edit* Fixed the Formatting of the sheet worker so future readers can see easier. 7/4/19 10:00 AM MST