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

setAttrs does not work with variables

Hello, I am trying to make a new character sheet and for some reason setAttrs does not work if I try to set it dynamically this is my code:     //If an ability changes make sure they can't go above the potential     skillList . forEach ( skill => {         on ( `change: ${ skill } -grade` , ( info ) => {             // Get pure skill name             const skillName = info . triggerName . substring ( 0 , info . triggerName . length - "-grade" . length );             // skill grade name             const skillGradeName = info . triggerName . toString ();             console . log ( "Skill changed: " + skillName );             //Get variables             getAttrs ([ skillGradeName , skillName + "-potential" ], function ( values ) {                 var skillGrade = parseInt ( values [ skillGradeName ]);                 var skillPotential = parseInt ( values [ skillName + "-potential" ]);                 //Console log reveals that variables are correct                 console . log ( `Skill grade: ${ skillGrade } | skill potential: ${ skillPotential } ` );                 if ( skillGrade > skillPotential ){                     //Everything still looks great here                     console . log ( `Skill grade: ${ skillGrade } is higher than skill potential: ${ skillPotential } ` );                     console . log ( skillGradeName );                      //Does not work                     setAttrs ({ skillGradeName: skillPotential }); ----------------------------------------------------------------------------------------------------------------------                     //Does not work let setObj = {};                     setObj . skillGradeName = skillPotential                     setAttrs ({ setObj }); ----------------------------------------------------------------------------------------------------------------------                     //Does not work either let setObj = {};                     setObj [ skillGradeName ] = skillPotential                     setAttrs ({ setObj }); ----------------------------------------------------------------------------------------------------------------------                     //this works                     setAttrs ({ "leadership-grade" : skillPotential });                 }             })         });     }) As you see I logged the skillGradeName - it outputs "leadership-grade". When I copy the string and try to hardcode it instead, it works! I bet the answer to this is something stupid/obvious I missed but could you please help me find it? What am I doing wrong? I've seen similar questions posted but none of the ones I found were quite the same and their solutions did not work for me (as you can see I tried several)
1642280324

Edited 1642280360
Nic B.
Roll20 Team
You're almost there on the first two, but the syntax is slightly wrong. In your first instance, you would want to do: setAttrs({ [skillGradeName]: skillPotential }); The [ ] allows javascript to interpolate the variable into the key. In the second instance, you would want to do: setAttrs(setObj) Set obj is already an object, so you don't need to wrap it with { } Hope this helps!
Thank you very much for the help. I knew it was something minor like this.