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

JavaScript syntax assistance please.

1599100519

Edited 1599100661
Steve
Plus
Sheet Author
Hey again guys, I've been trying to error check this myself, but can't work out the solution. Can you please tell me why this doesn't work please?  on("change:guardcurrent sheet:opened", function() { getAttrs(["attack","defence","evasion","guardcurrent"], function(value) { let atk = parseInt(value.attack)||0; let def = parseInt(value.defence)||0; let eva = parseInt(value.evasion)||0; let gua = String(value.guardcurrent)||"Neutral"; if (gua === "Hawk") { let atk = atk + 1, def = def - 2, eva = eva; } else if (gua === "Snake") { let atk = atk + 1, def = def - 3, eva = eva; } else if (gua === "Wolf") { let at k= atk + 2, def = def - 3, eva = eva - 1; } else if (gua === "Bear") { let atk = atk - 2, def = def + 1, eva = eva ; } else if (gua === "Cat") { let atk = atk - 2, def = def + 1, eva = eva + 1; } else if (gua === "Crab") { let atk = atk - 2, def = def + 2, eva = eva; } else if (gua === "Neutral") { let atk = atk, def = def, eva = eva; } setAttrs({ "attackmodded": value.atk; "defencemodded": value.def; "evasionmodded": value.eva; }); });              <label>Martial Guards</label>         Guard Taken <select name="attr_guardcurrent">             <option value="Neutral" selected>Neutral (0)</option>              <option value="Hawk">Hawk (1)</option>              <option value="Snake">Snake (2)</option>              <option value="Wolf">Wolf (3)</option>             <option value="Bear">Bear (4)</option>              <option value="Cat">Cat (5)</option>              <option value="Crab">Crab (6)</option>         </select> <div class="atkdefattributes">     <div class="2colrow">         <div class="col">         <label> Attack</label>         <label> Modified Attack</label>         <label> Defence</label>         <label> Modified Defence</label>         </div>         <div class="col">         <label><input type="number" name="attr_attack" value="" /></label>         <label><input type="number" name="attr_attackmodded" value="" /></label>         <label><input type="number" name="attr_defence" value="" /></label>                                 <label><input type="number" name="attr_defencemodded" value="" /></label>         </div> </div> </div> <div class="evaattribute">     <div class="2colrow">         <div class="col">         <label> Evasion</label>         <label> Modified Evasion</label>         </div>         <div class="col">         <label><input type="number" name="attr_evasion" value="" /></label>         <label><input type="number" name="attr_evasionmodded" value="" /></label>         </div>
1599109250

Edited 1599110374
GiGs
Pro
Sheet Author
API Scripter
Your first problem: you have a let variable assignment inside each of the if block. This is creating new, separate variables, that are not the same as the ones defined before the if, even though they have the same name. the let keyword creates variables that are "block scoped" - for practical purposes, that means they are erased once the block they are in ends. The solution to that is to simply remove the word let from the start of each group inside the if statements. The second problem is your setAttrs statement is assigning value.atk (and the others), but value doesnt include a value named atk. You should remove the value. part from each of the things inside setAttrs. Edit:  you also have a critical typo in the Wolf statement, which iwll crash the sheet worker.  let at k= atk + 2, that space between at and k is causing it to crash. And your setAttrs function is missing its ending }); section, and has semi-colons after each value. It should be like this:             setAttrs({ "attackmodded": atk, "defencemodded": def, "evasionmodded": eva             }); }); });
1599111025

Edited 1599111468
GiGs
Pro
Sheet Author
API Scripter
Here's an alternate way to handle this: on('change:guardcurrent change:attack change:defence change:evasion sheet:opened', function() {     getAttrs(['attack','defence','evasion','guardcurrent'], function(value) {                  const forms = {             Hawk: {attackmodded: 1, defencemodded: -2, evasionmodded: 0},             Snake: {attackmodded: 1, defencemodded: -3, evasionmodded: 0},             Wolf: {attackmodded: 2, defencemodded: -3, evasionmodded: -1},             Bear: {attackmodded: -2, defencemodded: 1, evasionmodded: 0},             Cat: {attackmodded: -2, defencemodded: 1, evasionmodded: 1},             Crab: {attackmodded: -2, defencemodded: 2, evasionmodded: 0},             Neutral: {attackmodded: 0, defencemodded: 0, evasionmodded: 0}         };         let gua = value.guardcurrent || 'Neutral';         const output = forms[gua] || forms.Neutral;         output.attackmodded += (parseInt(value.attack) || 0);         output.defencemodded += (parseInt(value.defence) || 0);         output.evasionmodded += (parseInt(value.evasion) || 0);         setAttrs(output);     }); }); This allows you to avoid the big if statement, and if you ever add new forms, its a simple matter of just adding them to the list with no other changes necessary. Note: I added the attack, defence, evasion attributes to the change line. In your original form, the modded attributes wouldnt change if the base attributes did.  You also dont need to use the String() function on gua. Attributes are always strings by default in roll20.
1599434495
Steve
Plus
Sheet Author
Thanks GiGs, Java scripting is way, way harder than the HTML. I really appreciate the support you give here. Thanks for taking the time to explain why things don't work rather than just dump an answer, I feel like I am actually learning each time I ask here.
1599457059
GiGs
Pro
Sheet Author
API Scripter
I'm glad to hear it :)