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

Multiple IF instances

Hi all,  very noob here when it comes to API scripting and all. In our game, for every 1/4 of the Hit Points you lose, you get a -1 penalty. I've set up the Bar1 (green) to be HP value and Bar3 (red) to be the penalty you get.  I was trying to figure a way of making a script that would automatically modify Bar3 to -1, -2 and -3 for every 1/4 drop in your Bar1 value. Is that possible? To have multiple IF instances? I've started with something like this: on("change:graphic", function(obj) {     if(obj.get("bar1_max") === "") return;         if(obj.get("bar1_value") <= obj.get("bar1_max") *0.75) {obj.set({"bar3_value": -1});} But when I add another IF (ie: if(obj.get("bar1_value") <= obj.get("bar1_max") *0.5) {obj.set({"bar3_value": -2});}, there's no response.  Sorry if this is too dumb, but I'd love to get some light on this :) Cheers
1586768247

Edited 1586772790
GiGs
Pro
Sheet Author
API Scripter
There are multiple ways to do this. First thing though is you want to limit the event to just changes in bar1 on("change:graphic:bar1_value", function(obj) { You can chain multiple IFs together, ideally using else , like this on("change:graphic:bar1_value", function(obj) {     // get the max and value as variables, to make later lines easier to write     let max = parseInt(obj.get("bar1_max")) || '';     if(max === '') return;     // all properties are naturally strings; we need to convert them into numbers     // to do the comparisons properly.     let value = parseInt(obj.get("bar1_value")) || 0;     let bar3 = parseInt( obj.get("bar3_value")) || 0;     let modifier = 0;     if(value <= (0.75 * max)) {         modifier = -1;     } else if (value <= (0.5 * max)) {         modifier = -2;     } else if (value <= (0.25 * max)) {         modifier = -3;     }     // now we have the modifier, check if it has changed     if (modifier !== bar3) {          obj.set({"bar3_value": -1});     }; });
Thanks for the swift reply :) I'll try this out. Cheers
Unfortunately, I get a  SyntaxError: Unexpected identifier Not sure what does that mean, though.
1586772798
GiGs
Pro
Sheet Author
API Scripter
I missed the opening bracket on two of the if statements - typos are very easy when writing code without testing :) I corrected the original post.
Thanks a lot. It didn't give me any error message now. But… It's not exactly what I wanted. I needed the bar3_value to show the modifier. I think the last line of the code actually makes it stay at -1 no matter what the actual modifier was.  if (modifier !== bar3) {          obj.set({"bar3_value": -1}); I really appreciate all the attention given, Gigs.
1586774704

Edited 1586774742
GiGs
Pro
Sheet Author
API Scripter
Oh yes, I just copied it from your original macro and meant to edit it to this:     if (modifier !== bar3) {          obj.set({"bar3_value": modifier});     }; That checks to see what bar3 is currently, and if the modifier your calculation builds is different, sets it to the new value. This is (ironically considering my error) so the modifier isnt being reset with the same value over and over. Lets say bar1 is at 74% of max, then drops to 54% of max. Both of those generate the same modifier, but the original code would run obj.set again anyway. Updating values on a character sheet or token is the slowest operation you'll do in your code, so you want to only do it when necessary.  In practice, it doesnt make a noticeable difference when you change one tokenbar. But when the campaign builds up,m and you have a lot of scripts or sheet workers active, and multiple players all doing things at the same time, these things add up and can cause noticeable lag. So its best to be as efficient as you can from the start.
Thanks for the tip as well. This, from my point of view should've worked but it doesn't. Still, in the end, I get only a -1 on the red bar, no matter the values of the modifiers. I think I'm gonna call it a day on this as I don't see why it wouldn't work. I'm really thankful for your attention, but I don't want to waste more of your time. I can input those values manually for now and think about this script later on when I get more experienced with scripting. Cheers!
1586776116
GiGs
Pro
Sheet Author
API Scripter
Don't worry, you're not wasting my time, I like helping out with code. (Even if, lol, I mess it up when I'm tired.) It looks like this is my mistake, the if statement was built backwards. An important thing to realise when doing if/else, is the code stops at the first matching value. If you look at my original code, it checks if the value is below 75% first, and sets the modifier to -1. But it then skips past the rest of the if statements because it found a matching value. The trick is to reverse the order, and do the biggest modifier first, like so     if(value <= (0.25 * max)) {         modifier = -3;     } else if (value <= (0.5 * max)) {         modifier = -2;     } else if (value <= (0.75 * max)) {         modifier = -1;     }
Yes! That worked like a charm! Thank you so much. Really nice of you. 
1586776492
GiGs
Pro
Sheet Author
API Scripter
You're welcome :) Sorry for leading you astray a couple of times, hehe.
1586783049
The Aaron
Roll20 Production Team
API Scripter
Since you're dealing just with math, you don't actually need if checks: modifier = (4-(Math.ceil((value/max)*4))||0; =D
Ha! Thanks, Aaron. And thanks once more, GiGs. I really appreciate all the help and replies. Great community :)