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

just can't understand this API behavior.

Hello i am fairly new to this but i just cant find an explanation for this. here is a very simple script i put together to make sure that bar1 (HP) will never go over the max. ---------------------------------------------------- on("change:graphic", function(obj) { var v = obj.get("bar1_value"); var m = obj.get("bar1_max"); log(v + " " + m) if (v > m) { log("doing if " + v + ">" + m); v=m; obj.set("bar1_value", v); } else{} }) ------------------------------------------------------- the IF condition is very simple and clear. and for most of the tokens it works great. but just what the hell am i suppose to do whit a log like this?: -------------------------------------------------------- "5 10" "doing if 5>10" -------------------------------------------------------- as a result even if i put bar1_value less then max, the script will change it to max. maybe 5 is bigger then 10??? did they change that and didn't tell me?
1389631924

Edited 1389632021
aRotondi
Sheet Author
Just ran a quick test on that, it worked fine for me. Double check your spacing and get rid of the else{} clause, it's not necessary as it does nothing. also you may want to add a separate check to determine if the 'graphic' is a token, as drawings I think don't have the bar values and thus would cause the script to error out. EDIT: Also, thanks for the script. I didn't realize you could actually set someome's bar over max until now.
1389631942

Edited 1389631960
Try parseInt(obj.get("bar1_value"));
1389632493

Edited 1389632717
the question is not how to fix this but rather why does it act like this? it works for me to on most of the tokens but it has unexpected behavior. returning true for an if statement that is clearly wrong on the very basic level of math.... i don't know what to tell you guys. it renders the API practically useless. your welcome aRotondi. excuse my english
Doron B. said: the question is not how to fix this but rather why does it act like this? it works for me to on most of the tokens but it has unexpected behavior. returning true for an if statement that is clearly wrong on the very basic level of math.... i don't know what to tell you guys. it renders the API practically useless. This may be due to javascript not using 'defined' data types, so instead of doing something like the following: int v = 5; which clearly states that v is a number, javascript does this: var v = 5; which says to the compiler (thing that checks if you're script is valid) 'hey, see what types this can fit into'. This may be causing the compiler to see it as something other than a number (it happens, but its rare), which is why you will want to use Brandon's fix if you continue to have the issue. If it's still doing weird things after that, check to make sure v isn't the name of a variable in another script, as I am not sure how roll20 strings together it's individual scripts into one codebase, and it could be causing some scope issues. also I am sorry if I ever offend or say things bluntly, just trying to quickly diagnose your issue :).
i tip my hat to you good sirs. problem solved. forgive my impatiens. 10x
BTW can anyway think of a way for this script to log or whisper to the DM the amount of the HP change? if for example a player change his HP from 40 to 50 then a msg will say "player1 added 10 to his hero.?
1389736945

Edited 1389737050
Here Doron: on("change:graphic:bar1_value", function(obj, prev) { if (Campaign().get("initiativepage") == false && obj.get("controlledby") == "") return; var damageTaken = 0; var currentHp =0; var oldHp = 0; var max = 0; var charName = obj.get("name"); currentHp = obj.get("bar1_value"); oldHp = prev["bar1_value"]; max = obj.get("bar1_max"); if (currentHp > max) { currentHp = max; obj.set("bar1_value", currentHp); } damageTaken = currentHp - oldHp; if (damageTaken < 0 ) { sendChat(charName, "/w GM has taken " + -1 * damageTaken + " damage"); } if (damageTaken > 0 ) { sendChat(charName, "/w GM has healed " + damageTaken + " damage"); } }); Edit: Formatting
Witcdhcraft!
1389765139
Lithl
Pro
Sheet Author
API Scripter
aRotondi said: Doron B. said: the question is not how to fix this but rather why does it act like this? it works for me to on most of the tokens but it has unexpected behavior. returning true for an if statement that is clearly wrong on the very basic level of math.... i don't know what to tell you guys. it renders the API practically useless. This may be due to javascript not using 'defined' data types, so instead of doing something like the following: int v = 5; which clearly states that v is a number, javascript does this: var v = 5; which says to the compiler (thing that checks if you're script is valid) 'hey, see what types this can fit into'. This may be causing the compiler to see it as something other than a number (it happens, but its rare), which is why you will want to use Brandon's fix if you continue to have the issue. If it's still doing weird things after that, check to make sure v isn't the name of a variable in another script, as I am not sure how roll20 strings together it's individual scripts into one codebase, and it could be causing some scope issues. also I am sorry if I ever offend or say things bluntly, just trying to quickly diagnose your issue :). Javascript is a dynamic untyped language. It will treat variables as whatever it feels appropriate at the time, and in fact some JS libraries have pages of code committed to type coercion: essentially, tricking the runtime to treat an object as one type over another. This can occasionally be a problem with operators that do more than one thing. Is a + b adding two numbers, or concatenating two strings? When JS compares two strings, it compares the numeric values of the characters, and '5' (value 53) is greater than '1' (value 49), and I suspect the extra characters of the longer string (the '0' in '10' ) are simply ignored, thus the incongruity. (Similarly, 'Zebra' < 'aardvark' ) When both operands can convert to a number, JS ought to convert them to a number for the conversion (it's part of the spec), but over the years I have learned not to trust "ought to" when it comes to dynamic types. If I need a variable to be a certain type and I haven't already ensured it is that type in some way, I'll coerce it myself.
tnx allot Brian. that was an excellent explanation! Josh - would you be kind enogh to explain to me the first IF statment of your script? im not sure what "initiativepage" got to do with it and why do you chack if there is no controller and what do you return?
1389900942

Edited 1389901037
Looks like Brian's code is set up to ignore token changes in 2 situations: if (Campaign().get("initiativepage") == false // If the Campaign has no initiative setup (you're not in combat)... && obj.get("controlledby") == "") // and the object that changed isn't controlled by anyone.... return; // then just return nothing, i.e. don't continue with the rest of the script.
great tnx.
Probably not a good idea to have that part in there about the initiative page since there are many ways to take damage and be healed outside of combat when the turn order tracker would not be open.
1389933393

Edited 1389933542
Also the second part about the object being controlled by someone.... probably shouldn't be there either.. if I am correct GM has an overriding control on everything which wouldn't not show up in controlledby. So that code might not let this work on tokens that aren't set to be controlled by anyone specific, but still are controlled by GM. If you can think of any situation where you wouldn't want the code to limit the bars to the max value then you can test for that condition with some adjustment to that line of code. If you want it to happen all the time, just leave that line out.