How do I do a high score with Local Storage?

Get help using Construct 2

Post » Sat Aug 01, 2015 2:16 pm

Hi,
I looked through the manual on the local storage plugin and reviewed the previous high score tutorial on high score.

It goes something like this
global variable - Score and HighScore
Start of layout -Local storage - Check item "score" exist
-Local storage - Check item "highscore" exist

Touch- On touched sprite->Add 1 to Score

Local storage-on item "score "exist - set item "score"to Score
Local storage-on item "score " set - set item "score"to Score
Local storage-on item "score " missing - set item "score"to Score
Local storage-on item "score " get -get item "score"

Button - on Clicked - Set "highscore"to HighScore
->System - Score>HighScore - Local Storage - set "highscore"to Score
- set HighscoreText to ""&Score
->Else -System - set HighScore to 0
- set HighscoreText to ""&Score

I seemed to be unable to set the local storage for highscore to my global variable - HighScore. The global variable - HighScore remains 0 while I see the value in the Local storage key - highscore.
It also dont seem to be updating e.g if score is lower than Highscore, as it will override new value.

Can someone advise if I am setting one of the steps above incorrectly?

My 2nd question is, if I were to restart the layout, how do I retrieve the HighScore stored in the LocalStorage?
B
10
S
3
G
1
Posts: 19
Reputation: 761

Post » Sat Aug 01, 2015 8:16 pm

Image
something like this works

I'm not sure what some of your events mean to achieve.
For example on "score" set -> set item "score" to score... makes no sense. The value is already getting set by the set item action, and "on item set" just marks the point of time, when it's done - like running some actions after you've made sure that values have been passed to local storage.

On item score exists - set "score" to score? Why?

The current workflow seems to be as follows:

at start of layout -> (no matter what) -> localstorage "score" is set to the score global variable (which probably still is 0)

on button clicked -> local storage "highscore" always gets overwritten by current highscore (so it will become 0)
then score is higher than highscore, if the sprite was touched at least once, highscore becomes score and then you display the score, or if score is 0, you display 0.
So all you do on button click is display the score in your text.

Normally you would load the highscore at start, and if it's missing create it with the value 0.
Then on button click you would get the highscore. Then on item "highscore" get, you compare the itemvalue to score. If score is bigger, set a new highscore. If score is smaller than highscore, leave highscore untouched.
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 788
Reputation: 3,788

Post » Sun Aug 02, 2015 5:51 am

Many thanks for your example above. It works as what I was looking for!
I was probably confused as to how the logic works as I couldnt understand if the localstorage should store score and highscore..
B
10
S
3
G
1
Posts: 19
Reputation: 761

Post » Mon Aug 31, 2015 11:57 pm

mindfaQ Thanks for the example, it helped me as I am used to the old WebStorage plugin. Is there any real need for the ever tick command as it seems to work fine without it?
B
8
S
2
Posts: 146
Reputation: 1,132

Post » Fri Sep 04, 2015 10:01 am

So am I right in understanding that webstorage used to allow you to directly compare the stored value and now with the get and set conditions of localstorage you need to compare global variables and then based on those comparisons set a new high score?
B
59
S
9
G
1
Posts: 970
Reputation: 6,611

Post » Fri Mar 25, 2016 11:54 am

I don't have the local storage plugin what do I do to add it?
B
7
S
2
G
1
Posts: 22
Reputation: 1,074

Post » Sat Apr 16, 2016 9:19 am

@jamal12 you need to update your construct 2 into latest version. There should be local storage plugin to add in your editor
Try this template if you want to create a game like Brain Dots game !
Image
B
17
S
7
G
4
Posts: 16
Reputation: 3,299

Post » Sun Apr 17, 2016 6:05 am

yes thanks I got it now
B
7
S
2
G
1
Posts: 22
Reputation: 1,074

Post » Sun May 29, 2016 6:34 pm

Hi mindfaQ
I tried it on my handphone and work well
but when I close the application and I go back
highscore back to zero
how to make the highscore not reset when I close the application
B
3
Posts: 2
Reputation: 183

Post » Sun May 29, 2016 6:40 pm

kepret wrote:Hi mindfaQ
I tried it on my handphone and work well
but when I close the application and I go back
highscore back to zero
how to make the highscore not reset when I close the application


Here is my tutorial its up to date and a lot easier plus I have an example capx file included.

https://www.scirra.com/tutorials/5343/h ... -game-capx
Check out my awesome Android Game releases https://play.google.com/store/apps/deve ... ames&hl=en
B
38
S
18
G
24
Posts: 682
Reputation: 16,635

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 6 guests