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] comparison problems

1599957379
Yui V.
Pro
Sheet Author
Hello, I wrote a simple script for a character sheet, and the first half works but not the second: on("change:pv", function() {         getAttrs(["pv", "pv_max"], function(values) {             let mod = "[[(";             let ago = Math.round((values.pv_max / 8));             if (values.pv > ago) mod = "[[(";             else if (values.pv == ago) mod = "à l'agonie [[(-1+";             else if (values.pv < ago) mod = "à l'agonie [[(-1+";             else if (values.pv == 0) mod = "au bord de la mort [[(-2+";             else if (values.pv < 0) mod = "au bord de la mort [[(-2+";             setAttrs({ agomod: mod });         });     }); It seems obvious to me now that the problem is that the script ignores the last two else ifs because the second one (values.pv < ago) already covers their 'range' of values. So provided values.pv is not greater than ago, this script always returns "à l'agonie [[(-1+", even when the hitpoints (pv) are null or negative when it should return " [...] [[(-2+". Is there a way to set a range of values to be compared to ? like [ 1->ago ] ? Or are there any other way to make my concept work? Thank you, from a script newbie.
1599977077

Edited 1599977255
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Welcome to sheet building Yui. There's a few things at work in your script. The first is your choice of comparison operators. You are using ==  (and I assume !=  for not equal). Instead you should use ===  and !== . This is because the double version, which you are using, forces the types of things to match (e.g. "0"==0 will equal true). While this may seem desirable, the way in which it forces the two things being compared to match types causes some weird results to crop up. The triple version ( ===  or !== ) tests the types of the two elements to compare as well (e.g. "0"===0 will equal false). This comparison has completely reliable results making it much more desirable. The joke goes that you should only use ==  and !=  once you understand when to use them, which is never. So, now on to your actual question. In your comparison, you correctly identified the issue, that the first 3 if's cover all eventualities for pv vs ago. I think what you want is that regardless of how pv relates to ago, if it is less than or equal to 0 it should become equal to "au bord de la mort [[(-2+". If that's what you want, the solution is to switch the if's around, like so: on("change:pv", function() {     getAttrs(["pv", "pv_max"], function(values) {         let mod = "[[(";         let ago = Math.round((values.pv_max / 8));         if (values.pv <= 0) mod = "au bord de la mort [[(-2+";         else if (values.pv > ago) mod = "[[(";         else if (values.pv <= ago) mod = "à l'agonie [[(-1+";         setAttrs({ agomod: mod });     }); }); Note, I've also compacted some of your comparisons to use the less than or equal to operator (<=) as the results were the same for the two comparisons. And, one small code critique. It could just be me, but I find that using the full if syntax is much more readable than the shorthand if syntax. E.g.: if(values.pv <= 0){     mod="au bord de la mort [[(-2+"; }
1599985704
GiGs
Pro
Sheet Author
API Scripter
You probably also want to coerce pv and pv_max to numbers, since by default they are strings and doing arithmetic with them will likely fail. on("change:pv", function() {     getAttrs(["pv", "pv_max"], function(values) {         let pv = +values.pv || 0;         let pv_max = +values.pv_max || 0;         let mod = "[[(";         let ago = Math.round((pv_max / 8));         if (pv <= 0) {             mod = "au bord de la mort [[(-2+";         } else if (pv > ago) {             mod = "[[(";         } else if (pv <= ago) {             mod = "à l'agonie [[(-1+";         }         setAttrs({ agomod: mod });     }); }); I've added Scott's suggestion of using the full if syntax. Personally I prefer the shorthand version, but it's good practice to use the longhand version when still learning, because I guarantee you will make mistakes that cause crashes with the shorthand version (I have, many times).
1599998586
Yui V.
Pro
Sheet Author
Thank you! Changing the order works indeed! Thanks for your advice, you two!
1600020526

Edited 1600020543
vÍnce
Pro
Sheet Author
@Steve " The joke goes that you should only use  ==  and  !=  once you understand when to use them, which is never. " I === that.  ;-P