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 .
×
May your rolls be chill this holiday season!
Create a free account

Running a worker on a field in a repeating section

I have two fields inside a repeating section.  One is a number input.  The other I need to be the output of a function that is based off of the number input.  The function is a simple lookup function, which I've used many times.  The repeating element is new, and I believe that's the issue. Here are the two fields inside their repeating section: <fieldset class="repeating_skills"> <div class="skillsBodyGrid lightBlueBackground"> <div class="sklrnk blueBackground section"> <div class="fieldLabelCenter white blueBackground section">Rank</div> <input class="field manual25" name="attr_skill_rank" type="number" min='1'> </div> <div class="sklrkb blueBackground section"> <div class="fieldLabelCenter white section">Rank Bonus</div> <input name="attr_skill_rank_bonus" type="hidden" value="0"> <span class="field derived21" name="attr_skill_rank_bonus"></span> </div> </div> </fieldset> I've looked at the instructions and the forum, but it's still not working for me.  I've tried several variations.  Here is what I have now: on('change:repeating_skills:skill_rank', () => { getAttrs(['repeating_skills_skill_rank'], values => { const skl_rnk = int(values.repeating_skills_skill_rank); const skill_rank_bonus_table = [ /* 0 */ 0, /* 1 */ 30, /* 2 */ 35, /* 3 */ 40, /* 4 */ 45, /* 5 */ 50, /* 6 */ 60, /* 7 */ 70, /* 8 */ 80, /* 9 */ 90, /* 10 */ 95, /* 11 */ 100, /* 12 */ 105 ]; var skl_rnk_bon = 0; if (skl_rnk > -1 && skl_rnk < 13) const skl_rnk_bon = skill_rank_bonus_table[skl_rnk]; console.log('repeating_skills_skill_rank_bonus: ' + skl_rnk_bon); setAttrs({repeating_skills_skill_rank_bonus: skl_rnk_bon}); }); }); Does anyone see my error(s)?
1588984465

Edited 1588984497
GiGs
Pro
Sheet Author
API Scripter
Your code is pretty confusing here                 var skl_rnk_bon = 0; if (skl_rnk > -1 && skl_rnk < 13) const skl_rnk_bon = skill_rank_bonus_table[skl_rnk]; You should start the if statement on a new line. This looks like an error of scope. The error is most apparent if you break the code out into its non-abbreviated form: if (skl_rnk > -1 && skl_rnk < 13) {     const skl_rnk_bon = skill_rank_bonus_table[skl_rnk]; } Anything after an if statement is in its own scope. You can do this (and I often do) for shorter cleaner code: if (skl_rnk > -1 && skl_rnk < 13) skl_rnk_bon = skill_rank_bonus_table[skl_rnk]; When you have only one thing in an if statement, you dont need the { } brackets, but you should think of them as still being there. So, this line     const skl_rnk_bon = skill_rank_bonus_table[skl_rnk]; is inside the scope of the if statement. The problem here is when you declare variables inside a scope, using const or let, they exist only within that scope. Even if it has the same name as another variable, it is a different instance, and loses its value when you move on from the if statement. So when you reach this setAttrs({repeating_skills_skill_rank_bonus: skl_rnk_bon}); that version of skl_rank_bon doesnt exist, only the first version with a value of 0 still exists.  This is easily solved, just by removing the const from that earlier line. on('change:repeating_skills:skill_rank', () => {     getAttrs(['repeating_skills_skill_rank'], values => {         const skl_rnk = int(values.repeating_skills_skill_rank);         const skill_rank_bonus_table = [ /* 0 */     0, /* 1 */     30, /* 2 */     35, /* 3 */     40, /* 4 */     45, /* 5 */     50, /* 6 */     60, /* 7 */     70, /* 8 */     80, /* 9 */     90, /* 10 */    95, /* 11 */    100, /* 12 */    105         ];         var skl_rnk_bon = 0;          if (skl_rnk > -1 && skl_rnk < 13)             skl_rnk_bon = skill_rank_bonus_table[skl_rnk];         console.log('repeating_skills_skill_rank_bonus: ' + skl_rnk_bon);         setAttrs({repeating_skills_skill_rank_bonus: skl_rnk_bon});     }); }); Or, for more clarity, add the scope brackets to the if statement: on('change:repeating_skills:skill_rank', () => {     getAttrs(['repeating_skills_skill_rank'], values => {         const skl_rnk = int(values.repeating_skills_skill_rank);         const skill_rank_bonus_table = [ /* 0 */     0, /* 1 */     30, /* 2 */     35, /* 3 */     40, /* 4 */     45, /* 5 */     50, /* 6 */     60, /* 7 */     70, /* 8 */     80, /* 9 */     90, /* 10 */    95, /* 11 */    100, /* 12 */    105         ];         var skl_rnk_bon = 0;          if (skl_rnk > -1 && skl_rnk < 13) {             skl_rnk_bon = skill_rank_bonus_table[skl_rnk];         }         console.log('repeating_skills_skill_rank_bonus: ' + skl_rnk_bon);         setAttrs({repeating_skills_skill_rank_bonus: skl_rnk_bon});     }); });
1588985297

Edited 1588985371
Well, that was surprising.  If I'd have come back to this tomorrow, I'd have seen that.  I know better than to declare a const inside an IF statement, or with the same variable name as a variable.  Today though, I was expecting that the repeating section was the issue. Anyway, it's working now. Thanks again GiGs.