WebStorage for invalid key returns NaN instead of 0

Discussion and feedback on Construct 2

Post » Tue Oct 28, 2014 4:23 pm

The manual shows that when requesting a key that doesn't exist, the call will return a 0, however it's return NaN (not a number).

Is there a way I can test if the value is NaN?
B
22
S
4
G
1
Posts: 100
Reputation: 2,150

Post » Tue Oct 28, 2014 4:36 pm

Good point, there's not currently any particular way to do that. I'll update the manual and add a condition for the next build.

Edit: actually, it returns an empty string, not 0. If you try to convert an empty string to a number you'll get NaN. There are also existing conditions to check if a key exists, or you can check if the key returned an empty string before trying to convert to a number.
Scirra Founder
B
387
S
230
G
88
Posts: 24,251
Reputation: 192,464

Post » Tue Oct 28, 2014 8:27 pm

Ashley wrote:Good point, there's not currently any particular way to do that. I'll update the manual and add a condition for the next build.

Edit: actually, it returns an empty string, not 0. If you try to convert an empty string to a number you'll get NaN. There are also existing conditions to check if a key exists, or you can check if the key returned an empty string before trying to convert to a number.


Thanks... yeah, found a tutorial from September of this year and am now using the key exists calls to verify first... working like a charm.

Thanks!
B
22
S
4
G
1
Posts: 100
Reputation: 2,150

Post » Tue Oct 28, 2014 11:19 pm

Still checking for NaN would be useful. I encountered a similar scenario where I was looking for a number from a browser string and it wasn't being reported. That's not a bug or anything, the browser string was empty... but the result was NaN and there was no convenient way to test for it.

R
B
7
S
3
G
1
Posts: 184
Reputation: 1,352

Post » Wed Oct 29, 2014 6:22 pm

I think in that case, you should check for ""

@rho
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
52
S
22
G
18
Posts: 2,122
Reputation: 17,093

Post » Wed Oct 29, 2014 9:51 pm

@Aphrodite

I did something similar, I checked for string length less than 2. ( I knew the number I wanted was always bigger than 100). I wasn't sure if checking for "" exactly matched what I was getting as there might have been a special character in there or something.. however they are probably the same thing.

still getting NaN for the int of an empty string is acceptable... but I still think it's reasonable to check for the number NaN than check for the input condition.
B
7
S
3
G
1
Posts: 184
Reputation: 1,352


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 2 guests