How do I check if a variable is Nan ?

Get help using Construct 2

Post » Mon Apr 17, 2017 11:11 am

Hi,
It keeps saying me that 'x' variable is Nan, I checked everything, didn't find a problem..
Can i bypass it somehow ? Can i check if x is nan -> set x 0 ?
Thank you,
p.s I think it's becouse of localstorage...
B
11
S
3
Posts: 277
Reputation: 1,395

Post » Mon Apr 17, 2017 11:46 am

may be you have set "Type" to text instead of number
Image
B
64
S
39
G
42
Posts: 631
Reputation: 30,555

Post » Mon Apr 17, 2017 12:02 pm

You could do a variable check:

if yourVariable*0≠0 then set yourVariable to 0
B
57
S
19
G
9
Posts: 639
Reputation: 9,533

Post » Mon Apr 17, 2017 2:17 pm

Ah, I've had something similar happen with local storage - you need to make sure the local storage variable is created and has a value before you can do anything with it, otherwise it will register as Nan.

This is how I set it up, at the start of the main event sheet:

Code: Select all
System|On start of layout: Local Storage|check item "yourLSVariable" exists
Local Storage|On item missing: Local Storage|set item "yourLSVariable" to (whatever value you want)


I also do the following check for if the item already exists and save it to a global variable:

Code: Select all
Local Storage|On item exists: set global variable "yourGlobalVariable" to LocalStorage.ItemValue


I can then work off the global variable instead of referring to the LocalStorage. At the end of the session I can then save that global back our to local storage if needed.

Another related tip - avoid putting any local storage events on a Loader layout, if you have one; I believe that once the game has downloaded once, the loader layout doesn't always play, in which case it would skip all those events.
B
16
S
7
G
3
Posts: 396
Reputation: 3,700

Post » Mon Apr 17, 2017 3:18 pm

@Elliott
Woah mate, that is a smart one !!!! :))))) Good job with that (if works ) [i'll try it...].

@mekonbekon
Mine is kinda same :

Start -> check Localstorage 'variable' exists.
on variable exist -> get it...
on variable get ->set variable to Localstorage.value.
B
11
S
3
Posts: 277
Reputation: 1,395

Post » Mon Apr 17, 2017 3:19 pm

@Elliott
@mekonbekon
@luckyrawatlucky

Thank you for answering me, the simplest & fastest answer was Eliot's one.
He deserves a big beer, very very very smart my friend, thank you ;) (y) good job
B
11
S
3
Posts: 277
Reputation: 1,395

Post » Mon Apr 17, 2017 3:51 pm

@Radulepy

Just noticed that there is also a System event: General: Is number a NaN :)
B
16
S
7
G
3
Posts: 396
Reputation: 3,700

Post » Mon Apr 17, 2017 7:25 pm

@Radulepy

Try using int(LocalStorage.ItemValue) when setting a variable after retrieving a value from LocalStorage
Vote for all the Construct 2 games on Steam Greenlight - Find them here
B
32
S
9
G
7
Posts: 444
Reputation: 6,731

Post » Thu Apr 20, 2017 3:58 pm

@OddConfection
@mekonbekon
Thank you for the help guys.
B
11
S
3
Posts: 277
Reputation: 1,395


Return to How do I....?

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests