How do I do a high score with Local Storage?

  • 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?

  • 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.

  • 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..

  • 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?

  • 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?

  • I don't have the local storage plugin what do I do to add it?

  • jamal12 you need to update your construct 2 into latest version. There should be local storage plugin to add in your editor

  • yes thanks I got it now

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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

  • 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

  • > 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

    thank for your help

    I will try in my project

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)