How do I check if a variable is Nan ?

0 favourites
  • 9 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • 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...

  • may be you have set "Type" to text instead of number

  • You could do a variable check:

    if yourVariable*0≠0 then set yourVariable to 0

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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:

    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)[/code:3f4i0lfn]
    
    I also do the following check for if the item already exists and save it to a global variable:
    
    [code:3f4i0lfn]Local Storage|On item exists: set global variable "yourGlobalVariable" to LocalStorage.ItemValue[/code:3f4i0lfn]
    
    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.
  • 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.

  • 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

  • Radulepy

    Just noticed that there is also a System event: General: Is number a NaN

  • Radulepy

    Try using int(LocalStorage.ItemValue) when setting a variable after retrieving a value from LocalStorage

  • OddConfection

    mekonbekon

    Thank you for the help guys.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)