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

Problem checking if a repeating section has a skill

1525275783
David
Sheet Author
I want to check if a skill is in a repeating section if it is I want to increase a value if not I want to add the skill.  Problem is it is doing both it find a skill and increases but also  add it.  I have almost positive this is because getAttrs is asynchronous. Is there any hop if doing this? function incSkill(section,skill,value,stat){ getSectionIDs(section, function(idArray) { var found = false; if (idArray.length > 0) { _.each(idArray, function(currentID, i) { getAttrs([section+"_" + currentID + "_skill_name",section+"_" + currentID + "_skill_score"], function(values) { var curSkill = values[section+"_" + idArray[i] + "_skill_name"]; var curScore = parseInt(values[section+"_" + idArray[i] + "_skill_score"]); if (curSkill === skill){ setAttrs({[section+"_"+idArray[i]+"_skill_score"]:curScore+parseInt(value)}); found = true; } })(found); }); } if (found === false){ addSkill(section,skill,value,stat); } }); }
1525276642

Edited 1525276728
Natha
KS Backer
Sheet Author
API Scripter
Could be optimised probably with .map and a for loop, with a break if the skill is found (?), but something like that? (warning, not tested) function incSkill(section,skill,value,stat){ getSectionIDs(section, function(idArray) { var found = false; if (idArray.length > 0) { var fields = []; _.each(idArray, function(currentID) { fields.push(section+"_" + currentID + "_skill_name"); fields.push(section+"_" + currentID + "_skill_score"); }); getAttrs(fields, function(values) { var update = {}; _.each(idArray, function(currentID, i) { var curSkill = values[section+"_" + idArray[i] + "_skill_name"]; var curScore = parseInt(values[section+"_" + idArray[i] + "_skill_score"]); if (curSkill === skill){ update[section+"_"+idArray[i]+"_skill_score"] =  curScore+parseInt(value); found = true; } }); if (found) { setAttrs(update); } else { addSkill(section,skill,value,stat); } }); } }); }
1525280994
David
Sheet Author
It worked thanks, it saved me a lot of SAN points.
1525283359
Natha
KS Backer
Sheet Author
API Scripter
Cool. Glad to help :)