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

Post » Tue May 30, 2017 4:19 pm

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? :roll:
Bl4ckSh33p-Soft - Custom Software and Indie Games
B
56
S
17
G
6
Posts: 748
Reputation: 9,420

Post » Tue May 30, 2017 4:24 pm

Does it work as intended if you remove the "else" and put "is not Nan" instead?
B
43
S
23
G
21
Posts: 735
Reputation: 12,132

Post » Fri Jun 02, 2017 3:45 pm

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-Soft - Custom Software and Indie Games
B
56
S
17
G
6
Posts: 748
Reputation: 9,420

Post » Fri Jun 02, 2017 5:51 pm

@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.
Scirra Employee
B
5
S
2
Posts: 129
Reputation: 889

Post » Fri Jun 02, 2017 6:45 pm

NaN stands for Not a Number

When storing your local storage variable, put int (variablename) around it.
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,572

Post » Fri Jun 02, 2017 7:14 pm

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
Bl4ckSh33p-Soft - Custom Software and Indie Games
B
56
S
17
G
6
Posts: 748
Reputation: 9,420

Post » Fri Jun 02, 2017 7:18 pm

You should be using "Check item exists" first, then using "On item exists" and "On item missing" to get or initialize your value.
ImageImageImage
B
71
S
22
G
248
Posts: 3,761
Reputation: 138,073

Post » Fri Jun 02, 2017 7:22 pm

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. :)
Bl4ckSh33p-Soft - Custom Software and Indie Games
B
56
S
17
G
6
Posts: 748
Reputation: 9,420


Return to General Discussion

Who is online

Users browsing this forum: lutnt40, NetOne and 2 guests