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

Sheetwork help: can anyone explain me why it doesn't work?

1502719587
Marco M.
KS Backer
Sheet Author
API Scripter
Compendium Curator
If I save the sum as an attribute and I repeat the same function for every change (so instead of a compound on("change:x change:y ... I make a different event caller with the same function inside) it seems to work, but it doesn't when I use variables defined in the script. The code should just look if the different checkbox are checked (each one has value one) and if their sum is greater than 2 (this version of the code is simplified cause I wanted to print out in the box the result to check. <code> let checkstim=['stim1_chk','stim2_chk','hardstim1_chk','hardstim2_chk','exhaustion_chk']; on(checkstim.map(str => `change:${str}`).join(' '),function(){ getAttrs(checkstim,function(v){ let sum=0; for(let i=0;i<v.length;i++){ sum+=(parseInt(v[`${checkstim[i]}`],10) || 0); } setAttrs({stim_chk: sum}); }); }); </code> Also it would be useful to create the program in this form because for every checkbox I have a value with hidden modifier number, the norm of the product of the two vector (sum(chk*values), with check being 0 if unchecked and 1 if checked) would give me the total modifier for my rolls.  I could write a function for each on("change:mod_chk") but it would took forever and it would be almost impossible to debug if I change samething. Please I'm desperate
1502721838
Jakob
Sheet Author
API Scripter
I'm afraid I don't completely understand what you're saying (the last paragraph, in particular), because of some kind of language barrier... anyway, I can tell you what does not work with your code: v  is an object, not an array, so it does not have a length property. Try this instead (this is not how I would write it, but I'm trying to stay close to your code): let sum=0; for (let i=0; i < checkstim.length ; i++){ sum += (parseInt(v[ checkstim[i] ],10) || 0); } setAttrs({stim_chk: sum});
1502725173
Marco M.
KS Backer
Sheet Author
API Scripter
Compendium Curator
Thanks Jacob, it worked... after I corrected the other mistake... I forgot to put "attr_" in front of the test variable stim_chk
1502838635
Marco M.
KS Backer
Sheet Author
API Scripter
Compendium Curator
And sorry for the second paragraph I pulled an all nighter coding various stuff so I wasn't particularly lucid. Basically I wanted to create a square norm between two arrays: given two arrays of same length, one containing the modifiers to my rolls, roll1, the other containing the checkbox value (that can only be 0 for unchecked and 1 for checked), arr2, the result should be    on(change any element of arr2){    let sum=0     for (i=0; i<arr1.length; i++)        sum+= arr1[i]*arr2[i];    end    setAttrs({totalmodifier: sim})    } Do you know if any of the libraries supported by the worksheet worker has this method already implemented (like Math. or _.) Also, how would you have written the code? I'm new to JavaScript and most of my general technique from C++/C#/Fortran/Python... Don't work here. Plus I really like your code style, I'm basically trying to copy it in my scripts :D
1502903752
Lithl
Pro
Sheet Author
API Scripter
let sum = _.reduce(arr1, (memo, val, i) => (memo + val * arr2[i]), 0); Another option: let sum = _.chain(arr1) .zip(arr2) .map((val) => val[0] * val[1]) .reduce((memo, val) => (memo + val), 0) .value(); There's no-built in like Matrix.norm(arr1, arr2), though.
1502904712
Marco M.
KS Backer
Sheet Author
API Scripter
Compendium Curator
Wow, thanks Brian. Does this user defined formula work within the limitations of the worksheet script or I can only use it for an API?
1502912346
Lithl
Pro
Sheet Author
API Scripter
Sheet workers have access to Underscore.js, yes. The _.chain, zip, map, reduce, and value functions will all work fine. See&nbsp;<a href="http://underscorejs.org/" rel="nofollow">http://underscorejs.org/</a> for full documentation of the available Underscore.js functions.
1502957516
Jakob
Sheet Author
API Scripter
What Brian said! For completeness, and since you asked (though I would not necessarily copy my coding style, I'm a total amateur!), I would have written your original code like this: const checkstim=['stim1_chk','stim2_chk','hardstim1_chk','hardstim2_chk','exhaustion_chk']; on(checkstim.map(str =&gt; `change:${str}`).join(' '), () =&gt; { getAttrs(checkstim, v =&gt; { const sum = checkstim.reduce((m, e) =&gt; m + (parseInt(v[e],10) || 0), 0); setAttrs({stim_chk: sum}); }); }); What can I say, I like .reduce().