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

[Question] Can anyone see why this is doing this?

January 31 (11 years ago)

Edited January 31 (11 years ago)
DXWarlock
Sheet Author
API Scripter
I have a script that seems straight forward, check a token, if its not a player, and it has HP, set a red aura.
Problem its having is even with no HP set, it still sets the aura, and logging it returns exactly what the IF checks, and does it anyway.

on("change:token", function (obj) {
    var oC = getObj('character', obj.get("_represents"));
	if(oC != undefined) return;
	var eBar = parseInt(obj.get("bar3_value"));
	log('eBar1check '+eBar);
	if(eBar != NaN) {
		log('eBar2check '+eBar);
		obj.set({'aura1_radius': -0.3,'aura1_color': '#ff0000','aura1_square': false});
	}
});
When I log it, Ebar = NaN, and even the 'if' parts passes with it equalling NaN, and I get:

"eBar1check NaN"
"eBar2check NaN
"

anyone see why its passing the IF statement if Ebar == NaN?
February 01 (11 years ago)

Edited February 01 (11 years ago)
Lithl
Pro
Sheet Author
API Scripter

If bar# has nothing set, its value is '' (empty string). parseInt('') returns NaN.

Edit: I misread the question and the code almost entirely. As Stephen points out, NaN is only discoverable with the isNaN function.

February 01 (11 years ago)
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
See bullet point 2 Ten Oddities And Secrets About JavaScript
February 01 (11 years ago)
Ah, I should have known that... I had the problem come up with one of my scripts too.
February 01 (11 years ago)

Edited February 01 (11 years ago)
Chad
Sheet Author
Hi William,

I'm having a similar problem. I've been trying to write a function that will return the character ID of an anonymous object, or false if there is no character id. It can be extremely tricky to try to capture all of the possible ways that can error out.

Generally, for your situation, I would not just jump into the getObj. I'd do this check first:

if (obj.get('represents') == '')

That will identify an object that isn't assigned a character. In your case, use:

if (obj.get('represents') == '') {
//Do your thing
}


February 02 (11 years ago)
DXWarlock
Sheet Author
API Scripter
Ah thanks guys, it seems temperamental on when it wants to work.
I was copying code from the auto status script and it works fine there
var maxValue = parseInt(obj.get("bar" + opts.barId + "_max"));
var curValue = parseInt(obj.get("bar" + opts.barId + "_value"));
if(maxValue != NaN && curValue != NaN)
but not in the one above, but works fine in another I have using it...
But I did find
if(!isNaN(eBar)
works great, So i'm using that :)
And ill use that instead chad, its a lot cleaner than some of of my "if not, returns" on checking if its a character and has a name :)