How do I make highscore with localstorage ?

Get help using Construct 2

Post » Wed Apr 05, 2017 9:04 am

How do I make highscore with localstorage ?
why my highscore always NaN..
B
4
Posts: 1
Reputation: 204

Post » Wed Apr 05, 2017 9:22 am

Local storage is asynchronous, it doesn't run in the same structure of the event sheet.

Basically you need to request a key first, then access the value with a get key trigger. If you try to access the value right away, before the get key is triggered, it will return the string "0", which is not a number (NaN).


Proper use is explained clearly in the manual entry. https://www.scirra.com/manual/188/local-storage
Mistakes were made.
B
49
S
24
G
107
Posts: 1,581
Reputation: 60,303

Post » Wed Apr 05, 2017 9:58 am

@shuwand

How do I make highscore with localstorage ?

Add the LocalStorage object

Add global variables Score=0 and HighScore=0

Initialise your high score:
System: On start of layout
-> LocalStorage: Get item "HighScore"

LocalStorage: On item "HighScore" get
-> System: Set HighScore to int(LocalStorage.ItemValue)

In your level end event (where you'd show the high score and have a restart button), add a sub-event:
System: Compare Score > HighScore
-> System: Set HighScore = Score
-> LocalStorage: Set item "HighScore" to HighScore

why my highscore always NaN..

Use in() to turn the values returned from local storage into numbers, i.e. int(LocalStorage.ItemValue)
Vote for all the Construct 2 games on Steam Greenlight - Find them here
B
32
S
9
G
7
Posts: 444
Reputation: 6,731


Return to How do I....?

Who is online

Users browsing this forum: bluesun66, Kenthria, Malhog, plinkie and 1 guest