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

Returning '0' seems to break sheet sandbox environment.

1587663983

Edited 1587664030
Here's my character sheet code (snippets): <span>Encumbrance:</span> <input type="number" value="0" name="attr_encumbrance"/> <span>Base:</span> <input type="number" value="120" name="attr_movebase"/> <span>Combat:</span> <input type="number" value="40" name="attr_movecombat"/> <span>Charge/Run:</span> <input type="number" value="120" name="attr_moverun"/> // When total ENCUMBRANCE changes, update the character's movement values... on('change:encumbrance', function(eventInfo) { getAttrs (['encumbrance', 'movebase', 'movecombat', 'moverun'], function(values) { setAttrs(calcMovement(values.encumbrance)); }); }); // Helper function for calculating character's movement rates based on encumbrance function calcMovement(theEncumbrance) { // movement thresholds and values const thresholds = [10, 7, 5, 0]; const moveBase = [30, 60, 90, 120]; const moveCombat = [10, 20, 30, 40]; const moveRun = [30, 60, 90, 120]; const index = thresholds.findIndex(element => theEncumbrance > element); const returnValues = {}; returnValues['movebase'] = moveBase[index]; returnValues['movecombat'] = moveCombat[index]; returnValues['moverun'] = moveRun[index]; return returnValues; } // When a hidden ENCUMBRANCE field's value changes, update the character's overall encumbrance... on('change:encarmor', function(eventInfo) { getAttrs (['encumbrance', 'encarmor'], function(values) { const newEncValue = values.encarmor; setAttrs({ Encumbrance : newEncValue }); }); }); I'm using the event handler and helper function to listen for a change in encumbrance and then set some movement values based on encumbrance thresholds. Everything works  except for the case where the encumbrance value is "0". When this is the case, the helper fails - obviously(?) because a '0' encumbrance value returns '-1' from findIndex(). I've tried everything I can think of to set the index=0 when it returns '-1', but the form still breaks.  And when I say break, depending on the approach, one or all of the movement fields stop responding and I have to completely refresh the sheet sandbox to get the character sheet functional again. I assume I'm dealing with a data type issue, but aren't familiar enough (yet) to be able to determine where and how to fix it. BTW, I've included the event handler that calculates the encumbrance value to begin with, at the bottom, in case the issues in there (when setting the sheet's attr_encumbrance value to '0').
1587664638
Andreas J.
Forum Champion
Sheet Author
Translator
have you tried adding some consolelog commands to post the values at different points to narrow it down where it fails, or when it gets wrong values?
Ugh, I found the problem - I was using const instead of let/var to define the index returned from findIndex(). When I later tried to change it (from '-1' to another value), that was a no-no with const . However, if I wanted to stick with const  how could I re-write the following more elegantly, if at all? let index = thresholds.findIndex(element => theEncumbrance > element); if (index == -1) {index = 3} (My apologies if this seems like a noob coding question, but it's been a long while since I did any serious javascripting...)
1587669671
GiGs
Pro
Sheet Author
API Scripter
You could rewrite that to use const, but it would involve some duplication, so you are better off leaving it as let. is there some significance to the index = 3? Knowing how your theEncumbrance array is built might provide a clue. Here is one way: // create a function for findIndex to avoid the next line being very hard to read const found = (mydata, find) => mydata.findIndex(element => find > element); // actually do the comparison, and then keep the value found or 3. const index = (-1 === found(thresholds, theEncumbrance)) ? 3 : found(thresholds, theEncumbrance); But it effectively involves doing the findIndex search twice, so for this leaving it at let is more efficient.