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 .
×

Help with negative numbers

Hello, To be clear I am a noob at writing scripts, but have done some research and I am still stumped. I have tried several different ways to convert a variable into a negative number but it always seems to end up as a positive number in my script. Is there a secret to this? Also, I am using an attribute and an attribute_max in the script and wondering if this is also an issue. I find when I set under to -12 things work as expected (so I think the syntax is ok, not totally sure) but if I try and convert the variable into a negative number it does not work. Any advice or help would be greatly appreciated. Also I am not 100% sure ab <!-- Status --> <script type="text/worker">     on("change:wounds change:choked change:endurance change:max_wounds sheet:opened", function() {           getAttrs(["WOUNDS","WOUNDS_MAX","ENDURANCE","CHOKED"], function(values) {         let max =  parseInt(values.WOUNDS_MAX)||0;         let wounds =  parseInt(values.WOUNDS)||0;         let endurance =  parseInt(values.ENDURANCE)||0;         let choked =  parseInt(values.CHOKED)||0;         let under = -max;                      if (wounds < under || choked < -5) {             status = "Dead";             //        } else if ((wounds < 1 && wounds >= (wounds_max * -1)) || (choked < endurance && choked > -6)) { //            status = "Unconscious"; //        } else if ((wounds < wounds_max && wounds > 0) || (choked < endurance && choked > 0)) { //             status = "Wounded";             } else {             status = "Healthy";             }                      setAttrs({                                       "health": status             });         });     }); </script> Thanks
1603779069
GiGs
Pro
Sheet Author
API Scripter
I'm not following your problem. What variable are you converting to a negative number? Some minor comments about the if expression: In one of your commented out lines you have this wounds >= (wounds_max * -1)) But wounds_max * -1 is also what under equals, so you could use under there fir a simpler expression. Also on that line you have && choked > -6 But the previous line checked against choked < -5. So only values over -6 can reach this line, so you can remove that.
1603779232
GiGs
Pro
Sheet Author
API Scripter
aha! In your if statement you refer to wounds_max in two places, but you never define that attribute. Instead you define let   max  =   parseInt ( values . WOUNDS_MAX )|| 0 ; So you will want to change those wounds_max references to max.
1603780972
GiGs
Pro
Sheet Author
API Scripter
I think  your if statement can be simplified like this:          if  ( wounds  <  under  ||  choked  < - 5 ) {              status  =  'Dead' ;         }  else   if  ( wounds  <  1  ||  choked  < 1 ) {              status  =  "Unconscious" ;         }  else   if  ( wounds  <  max  ||  choked  <  endurance ) {              status  =  "Wounded" ;         }  else  {              status  =  'Healthy' ;         } You dont have to check for a status that is already ruled out by a previous if.  For instance this line wounds < max && wounds > 0) has wounds > 0, but wounds must  be abovezero, because the previous line catches all cases of wounds below 0. You can only reach this line if wounds are above 0.
Hello GiGs, Thanks for your response. I think the simplification will be very helpful.  Sorry if the issue was not clear but the following expressions were not working as expected. let under = -max; (wounds_max * -1) I commented out the two middle lines to try and isolate the problem. With just the if statement and the else statements, understanding that wounds_max was 12, the status changed to Dead when the wounds were less than 0. I was expecting status to be dead when wounds were less than -12.
1603804881
GiGs
Pro
Sheet Author
API Scripter
Bahguerra said: With just the if statement and the else statements, understanding that wounds_max was 12, the status changed to Dead when the wounds were less than 0. I was expecting status to be dead when wounds were less than -12. That error was likely a result of wounds_max not being declared. Your sheet worker was crashing, but roll20 makes sure sheet workers crash silently, so it doesnt stop the whole character sheet from working.
Hello GiGs, Thanks again for all your help. There seems to still be an issue. I updated the script with the logic you proposed but I am still seeing the same issue. The choked status change works perfectly. so if CHOKED=6 and WOUNDS_MAX=12 coming in and I change the WOUNDS I get the following statuses; WOUNDS 1 or greater status is Healthy WOUNDS 0 status is Unconscious  WOUNDS -1 or less status is Dead So it looks to me like WOUNDS_MAX is somehow getting set to 1 or 0 Could it be the line  let max = parseInt(values.WOUNDS_MAX)||0; So, for some reason this is not coming back correctly and setting to 0? <!-- Status --> <script type="text/worker"> on("change:wounds change:choked change:endurance change:max_wounds sheet:opened", function() { getAttrs(["WOUNDS","WOUNDS_MAX","ENDURANCE","CHOKED"], function(values) { let max = parseInt(values.WOUNDS_MAX)||0; let wounds = parseInt(values.WOUNDS)||0; let endurance = parseInt(values.ENDURANCE)||0; let choked = parseInt(values.CHOKED)||0; let under = -max; if (wounds < under || choked < -5) { status = 'Dead'; } else if (wounds < 1 || choked < 1 ) { status = "Unconscious"; } else if (wounds < max || choked < endurance) { status = "Wounded"; } else { status = 'Healthy'; } setAttrs({ "health": status }); }); }); </script> Also, not sure if this matters but I am setting wounds_max from another script. <!-- Max Wounds --> <script type="text/worker">     on("change:strength change:endurance sheet:opened", function() {           getAttrs(["STRENGTH","ENDURANCE"], function(values) {         let str = parseInt(values.STRENGTH)||0;         let end = parseInt(values.ENDURANCE)||0;         let wound = str + end;         setAttrs({                                       "wounds_max": wound             });         });     }); </script>
1603808441
GiGs
Pro
Sheet Author
API Scripter
Can you post the html for the wounds, wounds_max, endurance, and choked attributes? might as well add the strength and endurance stats for good measure. Also you have multiple scrept blocks there. You should only have this line once in your html: <script type="text/worker"> So the above workers should be <script type="text/worker">     /* Status */ on("change:wounds change:choked change:endurance change:max_wounds sheet:opened", function() { getAttrs(["WOUNDS","WOUNDS_MAX","ENDURANCE","CHOKED"], function(values) { let max = parseInt(values.WOUNDS_MAX)||0; let wounds = parseInt(values.WOUNDS)||0; let endurance = parseInt(values.ENDURANCE)||0; let choked = parseInt(values.CHOKED)||0; let under = -max; if (wounds < under || choked < -5) { status = 'Dead'; } else if (wounds < 1 || choked < 1 ) { status = "Unconscious"; } else if (wounds < max || choked < endurance) { status = "Wounded"; } else { status = 'Healthy'; } setAttrs({ "health": status }); }); }); /* Max Wounds */ on("change:strength change:endurance sheet:opened", function() {           getAttrs(["STRENGTH","ENDURANCE"], function(values) {         let str = parseInt(values.STRENGTH)||0;         let end = parseInt(values.ENDURANCE)||0;         let wound = str + end;         setAttrs({                                       "wounds_max": wound             });         });     }); // all other sheet workers in here somewhere. </script>
1603809149

Edited 1603809235
GiGs
Pro
Sheet Author
API Scripter
Bahguerra  said: So it looks to me like WOUNDS_MAX is somehow getting set to 1 or 0 Could it be the line  let max = parseInt(values.WOUNDS_MAX)||0;  So, for some reason this is not coming back correctly and setting to 0? Yes, this is the issue.   The _max part must be in lower case. So your code should be getAttrs(["WOUNDS","WOUNDS_max","ENDURANCE","CHOKED"], function(values) { let max = parseInt(values.WOUNDS_max)||0; Also wherever the wounds_max is defined, should also have lower case, like so < input   type = 'text'   name = 'attr_WOUNDS_max'   value = '0'   > You can avoid issues like this by always  using lower case for attribute names.
Here are the stats. <label class='sheet-heavy'>Strength </label><input type="text" name='attr_strength' class='sheet-short'> <label class='sheet-heavy'>Endurance </label><input type="text" name='attr_endurance' class='sheet-short'> <label class='sheet-moderate'>Max Wounds </label><input type="text" name='attr_wounds_max' class='sheet-short' readonly> <label class='sheet-moderate'>Current Wounds </label><input type="text" name='attr_wounds' class='sheet-short'> <label class='sheet-moderate'>Max Endurance </label><input type="text" name='attr_endurance' class='sheet-short' readonly> <label class='sheet-moderate'>Choked </label><input type="text" name='attr_choked' class='sheet-short'>
GiGs said: Yes, this is the issue.   The _max part must be in lower case. So your code should be getAttrs(["WOUNDS","WOUNDS_max","ENDURANCE","CHOKED"], function(values) { let max = parseInt(values.WOUNDS_max)||0; Also wherever the wounds_max is defined, should also have lower case, like so < input   type = 'text'   name = 'attr_WOUNDS_max'   value = '0'   > You can avoid issues like this by always  using lower case for attribute names. I changed the reference in the script to WOUNDS_max and it is now working. Thanks GiGs for all your time and help!
1603810596
GiGs
Pro
Sheet Author
API Scripter
You're welcome! Since your attributes are defined as lower case in the inputs (which is good!), you should use lower case in your sheet workers too.