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

Using Variables in getAttrs and setAttrs instead of "written" text???

Hi All, I've just started trying to make my own custom character sheet. All has been going well (its been a good 10 years since I last dabbled in HTML and javascript). But I've become stuck trying to automate things a little too far maybe. What I'm trying to do is write a function that can update any attribute when you pass it in the name of the attribute and the value you want to change it by. I could write specific code for each attribute "hardcoded" (which would mean once for every skill and weapon skill, so lots of repeated code), but I would prefer to only have to write it once at a generic level and use that instead. So for my test case, I have a checkbox, when ticked it will change the value of the Defence Modifier attribute by 20, then unticking it will remove the 20. Ive got this working if I "hard code" the names of the attributes, as seen below... on("change:hasshield", function(eventInfo) { console.log("************ Has Shield Changed ************"); var isChecked = eventInfo.newValue; getAttrs(["defenceOtherMods"], function(pvalue) { var otherMods = parseInt(pvalue.defenceOtherMods); //If the box has been ticked, add the shields modifier if (isChecked == 1) {   otherMods = otherMods + 20; } else {     //Shield isn't ticked, so remove the bonus     otherMods = otherMods - 20; } setAttrs({ acrobatOtherMods: otherMods});   }); }); As I said what I want to be able to do is write a function that takes the name of the skill and value and updates that attribute, as shown below: <script type="text/javascript"> on("change:hasshield", function(eventInfo) { console.log("************ Has Shield Changed ************"); var isChecked = eventInfo.newValue; //If the box has been ticked, add the shields modifier if (isChecked == 1) {             updateSkillsModifiers("defence", 20); } else {     //Shield isn't ticked, so remove the bonus     updateSkillsModifiers("defence", -20); } }); //Update the given skill's otherMods value by the value given function updateSkillsModifiers(skillToUpdate, value) { var skillName = skillToUpdate + "OtherMods";     //PROBLEM LINE ! will getAttrs([skillName] actually work or does it need to be a "written" string getAttrs([skillName], function(pvalue) { console.log("Skill to update is " + skillName); var otherMods = parseInt(pvalue.skillName); console.log("othermods is " + otherMods); //Update the modifier value by the value given otherMods = otherMods + value;         //PROBLEM LINE ! Dont know how to use the var skillName instead of having to actually right the whole attribute setAttrs({ defenceOtherMods: otherMods});   updateAcrobatTotal();   }); } As you can see there are two issues that I'm finding. 1) Will getAttrs([skillName] actually work when using a string variable or does it need to be a "written" string? (hope that makes sense) 2) I don't know how to use the var skillName instead of having to actually right the whole attribute out again. So I'm wondering is there a way to do the above or am I trying the impossible? Thanks!
1585158749
LongShadow
Pro
Sheet Author
I am trying to do something similar using a string generated from an array via a for loop. The string looks right, but it's figuring out how to feed that information into the getAttrs function that's hampering me at the moment.
1585160271

Edited 1585160444
Caden
Forum Champion
Sheet Author
API Scripter
Compendium Curator
` pvalue[skillName]` is what you want for #2. I haven't tested this but you're looking for something more like this: Edit: I realized I didn't add your check in there to +/- 20. That should be easy enough to do. ['hasshield'].forEach(attr => on(`change:${attr}`, eventInfo => updateSkillsModifiers(attr, eventInfo.newValue))) const updateSkillsModifiers = (attr, newValue) => { const skillName = `${attr}OtherMods` getAttrs([skillName], value => { let otherMods = parseInt(value[skillName]) || 0 otherMods = otherMods + newValue setAttrs({ [`${skillName}`]: otherMods});   updateAcrobatTotal(); }) }
1585173313
GiGs
Pro
Sheet Author
API Scripter
Not directly answering your question, but look at&nbsp; <a href="https://wiki.roll20.net/UniversalSheetWorkers" rel="nofollow">https://wiki.roll20.net/UniversalSheetWorkers</a> &nbsp;for some other techniques to handle similar calculations.