
I have a lot of characteristics (ie STR, SIZ, INT) that have "attr_str_modchange", and "attr_siz_modchange" attributes associated with them. These "_modchange" attributes get changed when the associated "_current" attribute changes. The "_modchange" serves only to detect a change that might affect skills (which have modifiers based on the characteristics). I registered an "on change" function for each characteristic's "_modchange". When that fires it iterates over the array of skill names (ie "throw", "art1") and gets the associated "_modstring" from each of them, which will look something like "STR/SIZ/DEX/POW" or "DEX,INT///". The change handler then parses the "_modstring" by splitting it on "/" and "," and does a getAttr on a list of modifier attributes associated with each skill. It then recalculates the modifier for the skill by adding up the modifier attributes and setAttr to the "_mods" attribute associated with each skill. Believe it or not, this all actually works. Sort of. It only ever seems to work for the last skill in the array of skill names. I'm going to share some bits here, but I may need to share the full sheet with someone. I've been banging my head against this for several hours now and I think it may be down to asynchronicity or something. /*If Characteristic _modchange changes, recalc mods for affected skills*/
allCharacteristics.forEach(function (charName) {
on("change:" + charName + "_modchange", function() {
var logprefix = "*** Characteristic " + charName + "_modchange change: ";
console.log(logprefix + "recalc affected skills");
console.log(logprefix + "allSkills: " + allSkills.toString());
for (var sk = 0; sk < allSkills.length; sk++) {
var skillName = allSkills[sk];
console.log(logprefix + "does " + skillName + " at index " + sk + " need to have it's mod recalculated?");
getAttrs([skillName + "_modstring"], function(modstrings) {
console.log(logprefix + "In gettAttrs for _modstring for skill: " + skillName);
console.log(logprefix + "skillName: " + skillName + ", modstrings length: " + modstrings.length);
if (typeof modstrings === "undefined")
{
console.log(logprefix + "Skill " + skillName + " modstrings is undefined after getAttrs");
}
else {
console.log(logprefix + "skillName: " + skillName + ", modstrings: " + modstrings);
var modString = modstrings[skillName + "_modstring"];
/*It seems like it either comes through here twice, once with modString === undefined
and then another time with modString with a real value, or it's somehow doing both
sides of the if statement. Very odd.*/
if (typeof modString === "undefined"){
console.log(logprefix + "Skill " + skillName + "_modstring is undefined");
//setAttrs({[allSkills[sk] + "_mods"]: 0});
}
else{
console.log(logprefix + "Skill " + skillName + " has modString " + modString);
if (modString.toLowerCase().includes(charName)) {
var charMods = [];
var modStringSplit = modString.toLowerCase().split("/");
var modPrimary = modStringSplit[0].split(",");
var modSecondary = modStringSplit[1].split(",");
var modNegSecondary = modStringSplit[2].split(",");
var modNegPrimary = modStringSplit[3].split(",");
for (var mp = 0; mp < modPrimary.length; mp++) {
charMods.push(modPrimary[mp] + "_modprimary");
}
for (var ms = 0; ms < modSecondary.length; ms++) {
charMods.push(modSecondary[ms] + "_modsecondary");
}
for (var mns = 0; mns < modNegSecondary.length; mns++) {
charMods.push(modNegSecondary[mns] + "_modsecondary");
}
for (var mnp = 0; mnp < modNegPrimary.length; mnp++) {
charMods.push(modNegPrimary[mnp] + "_modnegprimary");
}
console.log(logprefix + "charMods to get for " + skillName + ": " + charMods.toString());
getAttrs(charMods, function(charModValues) {
var modTotal = 0;
for (var key in charModValues) {
if (charModValues.hasOwnProperty(key)) {
console.log(logprefix + "for allSkills[sk] " + skillName + ", charModValues property " + key + ", value " + charModValues[key]);
modTotal += charModValues[key];
}
}
console.log(logprefix + " " + skillName + " new Mods = " + modTotal);
setAttrs({[skillName + "_mods"]: modTotal});
});
}
else {
console.log(logprefix + "modString " + modString.toLowerCase() + " does not contain " + charName );
}
}
}
});
}
});
});