How do I save a high score?

Get help using Construct 2

Post » Mon Mar 27, 2017 6:54 pm

im making a small timing game and its almost finished now i trying to add a high score for the player. Problem is that every time the player loses and restarts the game i made all global variables reset! Is there a way to save scores and exempt a global variable on the reset so that the player what his previous score was?
B
6
S
1
Posts: 36
Reputation: 424

Post » Mon Mar 27, 2017 7:00 pm

Save your scores to local storage, rather than a global variable (or save to the global variable, and save the global variable to local storage).

On start of layout, load scores from local storage (if they exist) to global variable.

https://www.scirra.com/manual/188/local-storage
Mistakes were made.
B
53
S
27
G
114
Posts: 1,697
Reputation: 64,442

Post » Mon Mar 27, 2017 7:15 pm

Can anyone give me a guide or something im really like REALLY NEW at this i managed to make a game using global variables but this is kinda hard for me.
B
6
S
1
Posts: 36
Reputation: 424

Post » Mon Mar 27, 2017 7:21 pm

Mistakes were made.
B
53
S
27
G
114
Posts: 1,697
Reputation: 64,442

Post » Mon Mar 27, 2017 8:07 pm

@jakeedge02

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
Find all the Construct 2 games that were on Steam Greenlight here
B
35
S
9
G
7
Posts: 467
Reputation: 6,864

Post » Mon Mar 27, 2017 8:37 pm

Thanks guys i tried but but now the scores wont go up it keeps resetting back to 0
B
6
S
1
Posts: 36
Reputation: 424

Post » Mon Mar 27, 2017 8:51 pm

Are you loading the score from localstorage only on start of layout? Make sure you are not doing it every tick.
Mistakes were made.
B
53
S
27
G
114
Posts: 1,697
Reputation: 64,442

Post » Mon Mar 27, 2017 8:52 pm

oh yea i think it works now but the high score is isn't showing its shows NaN is it because i put the high score on a different layout?
B
6
S
1
Posts: 36
Reputation: 424

Post » Mon Mar 27, 2017 9:07 pm

System: Set HighScore to int(LocalStorage.ItemValue)

NaN means Not a Number. The int() part is important, as it changes a string into a number.
Mistakes were made.
B
53
S
27
G
114
Posts: 1,697
Reputation: 64,442

Post » Mon Mar 27, 2017 9:23 pm

Like this? I think im doing something wrong here please note that all global variables and the local storage is in a different layout

http://fav.me/db3r4ex
B
6
S
1
Posts: 36
Reputation: 424

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 43 guests