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

Firebase Error: Linking HP to a bar

I'm having an issue for _some_ of the characters in one of the D&D 4th Ed campaigns I'm in when their bar 3 is linked to the HP attribute (I like red for HP). I have an API event: on("change:graphic", function(obj, prev) that gets called 4 times when I modify the red circle for bar 3. In my testing, I've been doing -1 and +1. I'm trying to understand why it gets called 4 times, but the real problem is that one of those times, bar3_value and its previous counterpart are undefined. I do a parseInt and then check against NaN, but when I log the value, I still see NaN. The code looks roughly like this: var hpValStr = obj.get("bar3_value"); var hpVal = parseInt(hpValStr); if (hpVal !== NaN) { log(hpVal); } My output usually looks something like this: 33 NaN 33 33 Any insight here? My temporary solution was to remove the link between Bar3 and HP. The event only gets called called 1 time and with no error. If you'd like, I can post the firebase error.
1413211595
The Aaron
Roll20 Production Team
API Scripter
Do you have any other scripts running? Also, look for other copies of your script where the tab has no name; they will show up as an extra wide space between tabs, but when you point to them, they get a tab-like outline. If you post your full code, someone (probably me) can try and duplicate your issue and help you fix it. =D
1413216861
Lithl
Pro
Sheet Author
API Scripter
if (hpVal !== NaN) This line of code will always evaluate to true. In JavaScript, NaN != NaN . Use the isNaN function instead. if (!isNaN(hpVal)) { ... }
1413218583
The Aaron
Roll20 Production Team
API Scripter
Oh yeah! Dammit JS!!!! _.isNan() is the one I always use.. not sure if it's an important distinction.
1413221609

Edited 1413222060
Lithl
Pro
Sheet Author
API Scripter
_.isNaN = function(obj) { return _.isNumber(obj) && obj !== +obj; } _.isNumber = function(obj) { return toString.call(obj) === '[object Number]'; } Interestingly, _.isFinite always uses the native implementation of isNaN, not Underscore's implementation. Edit: Apparently, the native isNaN function returns true for undefined values as well, hence the reason for the isNumber call in Underscore's version of isNaN. Underscore's isFinite function calls isNaN(parseFloat(obj)), and parseFloat returns NaN when given an undefined value, so there is no need to call the custom isNaN function in that case. Also, I should mention that the isNumber code above isn't exactly how it's defined in Underscore; The full, copy-paste definition is: _.each(['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp'], function(name) { _['is' + name] = function(obj) { return toString.call(obj) === '[object ' + name + ']'; }; });
1413226983
The Aaron
Roll20 Production Team
API Scripter
good info!
1413229128
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
<a href="http://www.smashingmagazine.com/2011/05/30/10-oddi" rel="nofollow">http://www.smashingmagazine.com/2011/05/30/10-oddi</a>... Point number 2.... I was forever confused by NaN before I read that... not that it helped, but atleast I don't hurt myself with it any more.
1413237095
Lithl
Pro
Sheet Author
API Scripter
Stephen S. said: <a href="http://www.smashingmagazine.com/2011/05/30/10-oddi" rel="nofollow">http://www.smashingmagazine.com/2011/05/30/10-oddi</a>... Point number 2.... I was forever confused by NaN before I read that... not that it helped, but atleast I don't hurt myself with it any more. That article implies that it's JavaScript being ridiculous by calling NaN a number... but NaN being a number is part of the IEEE 754 floating-point specification. (In the IEEE spec, infinity is essentially 0^1111..., negative infinity is -0^1111..., and NaN is X^1111... where X can be anything except 0 or -0. This also means that a language can implement multiple different NaN values and still be correct.) The article's first point also complains that null is an object... but in most languages, only objects can be nullable, while value types can't be null. (In fact, C# has the Nullable class, in order to create value types that pretend to have null values!) The last point in the article is also implying that "undefined" has special meaning, and demonstrates that point by comparing a variable which has not been defined to "undefined" (getting the expected result, "true"). But that's not what's happening: the author isn't comparing an undefined variable someVar to a special value undefined -- he's comparing an undefined variable someVar to an undefined variable undefined . (This all has to do with JavaScript's "hoisting" of variables.) Whether "undefined" should be a reserved word, or whether undefined == undefined should be true are different arguments, but the author seems to be misunderstanding what he's writing there.
1413237451
The Aaron
Roll20 Production Team
API Scripter
Brian said: (In fact, C# has the Nullable class, in order to create value types that pretend to have null values!) When I worked at Thomson Reuters, we introduced a Nullable template for just that reason. Publishing financial data, we needed to know the difference between empty and not present. :)
Thanks, everyone. the isNaN() function worked great.