0 Favourites

Loading an empty value returns "NaN" even after chaging it

  • Hi. I load highscore from local storage at start but even after adding a check if its "NaN" it displays "NaN".

    + LocalStorage: On item "Highscore" get

    ----+ System: LocalStorage.?ItemValue is NaN

    -----> System: Set HIGHSCORE to 0

    ----+ System: Else

    -----> System: Set HIGHSCORE to LocalStorage.?ItemValue

    ----+ System: Trigger once

    -----> Highscore: Set text to HIGHSCORE

    Did I miss something or is it bugged? If empty is not "NaN" then it should show the empty value and not "NaN" in the text object?

  • Does it work as intended if you remove the "else" and put "is not Nan" instead?

  • I tried but it did not change. I added another check but its still NaN. :/

    + LocalStorage: On item "Highscore" get

    ----+ System: LocalStorage.?ItemValue is NaN

    ----+ OR System: LocalStorage.?ItemValue = ""

    -----> System: Set HIGHSCORE to 0

    ----+ System: [X] LocalStorage.?ItemValue is NaN

    -----> System: Set HIGHSCORE to LocalStorage.?ItemValue

    ----+ System: Trigger once

    -----> Highscore: Set text to HIGHSCORE

  • Bl4ckSh33p isn't the value coming back from localstorage a string? Try converting it to a number before assigning it to HIGHSCORE. NaN is a special number ( there's several special values in the IEEE standard for things like this and infinity ), so unless you attempt to convert an invalid value to a number it won't actually be the value of NaN.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • NaN stands for Not a Number

    When storing your local storage variable, put int (variablename) around it.

  • Its a global number which is stored. It only happens at the first start when no value is set and its loaded from storage. But I think I found a fix. Instead of comparing the value with the system action and checking if its NaN I used the local storage compare action:

    + LocalStorage: On item "Highscore" get

    ----+ LocalStorage: Value = ""

    -----> System: Set HIGHSCORE to 0

    ----+ System: Else

    -----> System: Set HIGHSCORE to LocalStorage.?ItemValue

    ----+ System: Trigger once

    -----> Highscore: Set text to HIGHSCORE

  • You should be using "Check item exists" first, then using "On item exists" and "On item missing" to get or initialize your value.

  • Right, I should have tried it this way earlier. I have On Start of Layout > Get Item to make it quick but checking it first would only Get Item if it exists. But it seems to work now with my "hack". Thanks for the suggestion.

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