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

Sheetworker/Javascript Help

1585027158

Edited 1585027278
I got this code from someone here (probably Jakob or The Aaron) and I am trying to adapt it to something else, but it is not working. const classids = ["01", "02", "03", "04"]; const skills = ["acrobatics", "appraise", "bluff", "climb", "craft1", "craft2", "craft3", "craft4", "craft5", "craft6", "disable_device", "disguise", "escape_artist", "gather_info", "handle_animal", "heal", "intimidate", "jump", "know_arcana", "know_arch", "know_dungeon", "know_geography", "know_history", "know_local", "know_nature", "know_nobility", "know_religion", "know_planes", "linguistics", "perception", "perform1", "perform2", "perform3", "perform4", "perform5", "perform6", "persuasion", "profession1", "profession2", "profession3", "profession4", "profession5", "profession6", "ride", "search", "sense_motive", "sleight_of_hand", "spellcraft", "stealth", "survival", "swim", "use_magic_device"]; const skillAttrs = _.flatten(_.map(classids,(c)=>_.map(skills, (s)=>`${s}_class${c}_points`))); const skillEvents = _.map(skillAttrs,(a)=>`change:${a}`); const pointsAttrs = _.map(classids,(c)=>`class${c}_skillpoints`); on(`change:skillpoints_class ${skillEvents.join(' ')}`, function() {   getAttrs((skillAttrs,pointsAttrs), (value) =>{     let update = {};         _.each(classids, (c)=>{             update[`skillpoints${c}_unspent`]=(parseInt(value[`class${c}_skillpoints`])||0) - _.reduce(                 _.map(skills, (s) => parseInt(value[`${s}_class${c}_points`])||0 ),                 (m,p) => m+p,                 0);         });     setAttrs(update);   }); }); I am pretty sure my error is in the bolded line. I have done logs and my constants all look like they should. I also can see that I get into the each loop when I should, but update does not seem to set. Any advice/assistance appreciated. Thanks!
1585031007

Edited 1585033995
GiGs
Pro
Sheet Author
API Scripter
That's a very complex function to try to bugfix. I would recommend trying not to do so much on a single line of code. break that line out to its constituent parts, and use console.log statements to make sure everything has the value it should have. Then put it back together. I would do something like this: on(`change:skillpoints_class ${skillEvents.join(' ')}`, function() {     getAttrs((skillAttrs,pointsAttrs), (value) =>{         let update = {};         _.each(classids, (c)=>{             let assignment_name = `skillpoints${c}_unspent`;             let startingskillpoints = parseInt(value[`class${c}_skillpoints`])||0;             let reducelist = _.map(skills, (s) => parseInt(value[`${s}_class${c}_points`])||0 );             let reduced = _.reduce(reducelist, (m,p) => m+p, 0);             let assignment_value = startingskillpoints - reduced;             update[assignment_name]=assignment_value;         });         setAttrs(update);     }); }); Then you can insert console.log statements to find out which value isnt being set properly. Do you have the original version of the code? That would make it easier to understand where you've gone astray.
1585031509

Edited 1585031529
GiGs
Pro
Sheet Author
API Scripter
Though immediately after posting, I noticed an error on this line getAttrs((skillAttrs,pointsAttrs), (value) =>{ Those are two arrays, and you cant just combine them like that. One old way would be to do getAttrs(skillAttrs.concat(pointsAttrs), (value) =>{ concat means concatenate - put those two arrays into one A more modern approach would be the spread operator. Using ...arrayname 'spreads' out the array into a list of its items. So you can do this: getAttrs([...skillAttrs, ...pointsAttrs], (value) =>{ Try these and see if they help.
Many thanks, GiGs. It was the concatenating that was the problem. Works like a champ now.