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

[HELP] part of a sheet worker don't work

1682697226

Edited 1682697989
Maïlare
Pro
Sheet Author
API Scripter
Hello, I have wrote a sheetworker to define the skills scores in terms of abilities score. T hieving  skills are defines only if the choosed class is thief or assassin. For this I have wrote two arrays, one with the general skills and another with thieving skills  . But I need to use getAttrs with function(setting) and function(values) for the thieving  skills   because I have to define if the choosed class is thief or assassin. How can I make this. For understand, here my actual code with the second part don't work :      const dexSettings = {         3 : { reactAttAdj: - 3 , defAdj: 4 },         4 : { reactAttAdj: - 2 , defAdj: 3 },         5 : { reactAttAdj: - 1 , defAdj: 2 },         6 : { reactAttAdj: 0 , defAdj: 1 },         7 : { reactAttAdj: 0 , defAdj: 0 },         8 : { reactAttAdj: 0 , defAdj: 0 },         9 : { reactAttAdj: 0 , defAdj: 0 },         10 : { reactAttAdj: 0 , defAdj: 0 },         11 : { reactAttAdj: 0 , defAdj: 0 },         12 : { reactAttAdj: 0 , defAdj: 0 },         13 : { reactAttAdj: 0 , defAdj: 0 },         14 : { reactAttAdj: 0 , defAdj: 0 },         15 : { reactAttAdj: 0 , defAdj: - 1 },         16 : { reactAttAdj: 1 , defAdj: - 2 },         17 : { reactAttAdj: 2 , defAdj: - 3 },         18 : { reactAttAdj: 3 , defAdj: - 4 }     };         const thievingSkills = {         9 : { pickpocket: '-15 \% ' , locks: '-10 \% ' , traps: '-10 \% ' , silently: '-20 \% ' , hiding: '-10 \% ' },         10 : { pickpocket: '-10 \% ' , locks: '-5 \% ' , traps: '-10 \% ' , silently: '-15 \% ' , hiding: '-5 \% ' },         11 : { pickpocket: '-5 \% ' , locks: 0 , traps: '-5 \% ' , silently: '-10 \% ' , hiding: 0 },         12 : { pickpocket: 0 , locks: 0 , traps: 0 , silently: 0 , hiding: 0 },         13 : { pickpocket: 0 , locks: 0 , traps: 0 , silently: 0 , hiding: 0 },         14 : { pickpocket: 0 , locks: 0 , traps: 0 , silently: 0 , hiding: 0 },         15 : { pickpocket: 0 , locks: 0 , traps: 0 , silently: 0 , hiding: 0 },         16 : { pickpocket: 0 , locks: '+5 \% ' , traps: 0 , silently: 0 , hiding: 0 },         17 : { pickpocket: '+10 \% ' , locks: '+10 \% ' , traps: 0 , silently: '+5 \% ' , hiding: '+5 \% ' },         18 : { pickpocket: '+5 \% ' , locks: '+15 \% ' , traps: '+5 \% ' , silently: '+10 \% ' , hiding: '+10 \% ' }     };     on ( 'change:dexBase change:class1 change:class2 change:class3' , function () {         getAttrs ([ 'dexBase' ], function ( setting ) {             const dexObj = dexSettings [ setting . dexBase ];             const setdexObj = {                 reactionAdj:dexObj . reactAttAdj ,                 missileAdj:dexObj . reactAttAdj ,                 defensiveAdj:dexObj . defAdj             };                         setAttrs ( setdexObj );         });         getAttrs ([ 'dexBase' , 'class1' , 'class2' , 'class3' ], function ( values ) {             const thiefObj = thievingSkills [ values . dexBase ];             var class1 = parseInt ( values . class1 );             var class2 = parseInt ( values . class2 );             var class3 = parseInt ( values . class3 );             if ( class1 === 10 || class2 === 10 || class3 === 10 || class1 === 11 || class2 === 11 || class3 === 11 ) {                 const setThiefObj = {                     pickPockets:thiefObj . pickpocket ,                     openLocks:thiefObj . locks ,                     removeFindTraps:thiefObj . traps ,                     moveSilently:thiefObj . silently ,                     hideInShadows:thiefObj . hiding                 };             }             setAttrs ( setThiefObj );         });     });
1682698662
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Hi Mailare, There are a couple code issues that I notice. The first one is probably what is causing your issue: You define setThiefObj  inside the if block, but then you use it outside the if block. Javascript scopes variables to the code blocks in which they are defined. A code block is usually  a set of code enclosed in {...} . You need to define your setObj outside of the if block, and then assign values to it in the if block. I would recommend doing all of this work in a single getAttrs with a single setAttrs. Doing this also fixes issue #1 as a side effect. Addressing both of those issues would look like this: const dexSettings = { 3: { reactAttAdj: -3, defAdj: 4 }, 4: { reactAttAdj: -2, defAdj: 3 }, 5: { reactAttAdj: -1, defAdj: 2 }, 6: { reactAttAdj: 0, defAdj: 1 }, 7: { reactAttAdj: 0, defAdj: 0 }, 8: { reactAttAdj: 0, defAdj: 0 }, 9: { reactAttAdj: 0, defAdj: 0 }, 10: { reactAttAdj: 0, defAdj: 0 }, 11: { reactAttAdj: 0, defAdj: 0 }, 12: { reactAttAdj: 0, defAdj: 0 }, 13: { reactAttAdj: 0, defAdj: 0 }, 14: { reactAttAdj: 0, defAdj: 0 }, 15: { reactAttAdj: 0, defAdj: -1 }, 16: { reactAttAdj: 1, defAdj: -2 }, 17: { reactAttAdj: 2, defAdj: -3 }, 18: { reactAttAdj: 3, defAdj: -4 } }; const thievingSkills = { 9: { pickpocket: '-15\%', locks: '-10\%', traps: '-10\%', silently: '-20\%', hiding: '-10\%' }, 10: { pickpocket: '-10\%', locks: '-5\%', traps: '-10\%', silently: '-15\%', hiding: '-5\%' }, 11: { pickpocket: '-5\%', locks: 0, traps: '-5\%', silently: '-10\%', hiding: 0 }, 12: { pickpocket: 0, locks: 0, traps: 0, silently: 0, hiding: 0 }, 13: { pickpocket: 0, locks: 0, traps: 0, silently: 0, hiding: 0 }, 14: { pickpocket: 0, locks: 0, traps: 0, silently: 0, hiding: 0 }, 15: { pickpocket: 0, locks: 0, traps: 0, silently: 0, hiding: 0 }, 16: { pickpocket: 0, locks: '+5\%', traps: 0, silently: 0, hiding: 0 }, 17: { pickpocket: '+10\%', locks: '+10\%', traps: 0, silently: '+5\%', hiding: '+5\%' }, 18: { pickpocket: '+5\%', locks: '+15\%', traps: '+5\%', silently: '+10\%', hiding: '+10\%' } }; on('change:dexBase change:class1 change:class2 change:class3', function () { getAttrs(['dexBase', 'class1', 'class2', 'class3'], function (values) { // Create an empty object to store our changes const setObj = {}; const dexObj = dexSettings[values.dexBase]; // Add the dex obj changes to the set obj Object.assign(setObj,{ reactionAdj: dexObj.reactAttAdj, missileAdj: dexObj.reactAttAdj, defensiveAdj: dexObj.defAdj }); const thiefObj = thievingSkills[values.dexBase]; var class1 = parseInt(values.class1); var class2 = parseInt(values.class2); var class3 = parseInt(values.class3); if (class1 === 10 || class2 === 10 || class3 === 10 || class1 === 11 || class2 === 11 || class3 === 11) { // assign the thief specific changes Object.assign(setObj,{ pickPockets: thiefObj.pickpocket, openLocks: thiefObj.locks, removeFindTraps: thiefObj.traps, moveSilently: thiefObj.silently, hideInShadows: thiefObj.hiding }); } // Note that you probably want handling for what to do with the thief specific skills if these class values are not the correct value. // Apply all changes at once setAttrs(setObj); }); }); The reason for doing this all in a single getAttrs - setAttrs loop is that this is the most efficient way to get and set attributes. Additionally, because both getAttrs and setAttrs are asynchronous, the divided approach you were using would not necessarily execute in the order you wrote it. I don't think the execution order would have been an issue in this case, but it's something to be aware of in the future.
1682699636

Edited 1682699688
Maïlare
Pro
Sheet Author
API Scripter
Thanks Scott, it works :D And thanks for the tips, I will try to remember this for my future sheetworkers.