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 : absolute noob setting attributes with if

1551026085
Pantoufle
Pro
Sheet Author
Translator
Hello I seek guidance on this matter because I'm absolute noob on this and cannot figure it out. I try to calculate a strength mod based on the strength value with the sheet worker. But it's not linear and I tried to handle things with "if" Well I haven't got any success... Since there are a lot of awesome people on the forum, I think it'll be quicker to ask rather than to solve it myself :p This is the code so far, I fiddled it a lot. The end purpose is to set the others stats also, but currently only working on strength :) THX for reading (not native speaker, sorry for any mistake) <script type="text/worker"> on("change:attr_strength change:attr_dexterity change:attr_constitution change:attr_intelligence change:attr_wisdom change attr_charisma sheet:opened", function() {    getAttrs(["attr_strength","attr_dexterity","attr_constitution","attr_intelligence","attr_wisdom","attr_charisma"], function(values) {         let stat1 = parseInt(values.attr_strength)||0;         let mod1;         if (stat1=3) {setAttrs({mod1=-3});}             else if (stat1<6) {setAttrs({mod1=-2});}             else if (stat1<9) {setAttrs({mod1=-1});}             else if (stat1<13) {setAttrs({mod1=-0});}             else if (stat1<16) {setAttrs({mod1=1});}             else if (stat1<18) {setAttrs({mod1=2});}         else {setAttrs({mod1=3});}     setAttrs({                                        "attr_strengthMod": mod1         });   }); }); </script>
1551037980

Edited 1551041383
GiGs
Pro
Sheet Author
API Scripter
You have a bunch of problems. The biggest: you are using attr_strength. You only use the attr_ part in the input or select where you create the attribute. After that you just use strength . So your on(change and getAttrs lines need reworking, to something like on("change:strength and  getAttrs(["strength", etc. Secondly, when comparing values, you need to use == or ===, not =. In javascript, the single = is only used to assign values. So that first if should be if (stat1 === 3) Finally, the setAttrs operation inside the if statement: You are making two mistakes here. Firstly, you should only have one setAttrs operation, at the end of the function, and secondly, the syntax is wrong. It would not be: setAttrs({mod1=-3}) It would be (notice its a colon not an equals sign). setAttrs({mod1:-3}) But as I said, you shouldn't be using setAttrs there. You should just be using mod1 =3 (that does use a single =, because its an assignment: you are setting a value.) Putting it together, that first equals line would be if (stat1 === 3) { mod1 = -3;} I included a few extra spaces for clarity. Now, one final thing: not essential, but you dont need to use curly brackets to enclose a single line of code. If the if statement had multiple lines, you would need them. But this is just a single line so it doesn't. So you could write that as if (stat1 === 3) mod1 = -3; That said, it does no harm to use the curly brackets, and being consistent helps to avoid confusion when you're starting out, so I'd recommend using them till you are more comfortable with javascript. I'll follow this post with a version of the your sheet worker that will calculate all your stats.
1551038876

Edited 1551041309
GiGs
Pro
Sheet Author
API Scripter
Here's an untested version that works for all stats: const stats = ['strength','dexterity','constitution','intelligence','wisdom','charisma']; stats.forEach(function (stat) { &nbsp; &nbsp; on("change:" + stat + " sheet:opened", function () { &nbsp; &nbsp; &nbsp; &nbsp; getAttrs([stat], function (values) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const score = parseInt(values[stat], 10)||0; // this extracts the stat, and assumes a stat score of 0 the stat is not recognised as a number. let mod = 0; if(score &lt; 9) mod = Math.floor((score-9)/3); else if(score &gt; 17) mod = 3; else if(score &gt; 12) mod = Math.ceil((score-12)/3); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setAttrs({ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [stat + 'Mod']: mod &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; }); }); It uses the techniques described on this page:&nbsp; <a href="https://wiki.roll20.net/UniversalSheetWorkers" rel="nofollow">https://wiki.roll20.net/UniversalSheetWorkers</a> . In brief, it loops through your stats (defined in the array) and creates a separate and unique sheet worker for each of them.&nbsp; It also uses the Math.ceil (round up) and Math.floor (round down) functions to make your if statement a little shorter, though perhaps a bit less readable. There's actually nothing wrong with the structure of your if statement, so you could use that instead, as long as you remember to use === in place of = for comparisons, and just use mod= instead of setAttrs({mod= within the if statement.
1551041062
Pantoufle
Pro
Sheet Author
Translator
Thx a lot GiGs, very helpful and really detailed :) I get it :) I'll let you know if it works :)
1551041582
Pantoufle
Pro
Sheet Author
Translator
Works like a charm except that for a score of 3 (or lesser) mod is actually -3... I just added an extra line in your code :)
1551042018
GiGs
Pro
Sheet Author
API Scripter
Great! And I see I should have spotted that :)