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
54
S
28
G
115
Posts: 1,705
Reputation: 65,083

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)
Find all the Construct 2 games that were on Steam Greenlight here
B
36
S
10
G
7
Posts: 475
Reputation: 7,015


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 8 guests