0 Favourites

How do I Highest score

  • Good morning,

    I'm trying to keep the highest score, the game is saved alone.

    I'm able to make you save the punctuation you do at that time, but if you then repeat and take out less saves you instead of leaving the highest.

    some help?

    Thank you

  • Create two global variables: g_score, g_hiscore.

    Add the Local Storage object to your project.

    Add this code to the start of your main event sheet:

    1. System|On start of layout: LocalStorage|Check item "savehiscore" exists

    2. LocalStorage|On item "savehiscore" exists: System|Set g_hiscore to LocalStorage.ItemValue

    3. LocalStorage|On item "savehiscore" missing: System|Set item "savehiscore" to 0

    This code checks whether you've already saved a hiscore to Local Storage - if you have it loads that value into your g_hiscore global variable, if you haven't it creates the item in Local Storage and then assigns it the value of 0.

    During your game session add whatever points you are scoring to g_score.

    At the end of the game session and before you start the next session add the following code into your event sheet:

    Edit: I forgot to include a trigger once:

    1Trigger Once:

    >>System| if g_score>g_hiscore: System|Set g_hiscore to g_score; LocalStorage| set item "savehiscore" to g_hiscore

    This code checks whether g_score is greater than g_hiscore and if so updates "savehiscore" in LocalStorage.

    If you've accessed other items in LocalStorage between retrieving "savehiscore" at the start and updating it at the end you may need to retrieve it again before updating (I think? Maybe someone can confirm)

    Hope that helps!

  • I've tried it and it has not worked for me, because what I want to keep is seconds.

    I can always keep the last score, but not the highest.

    thank you

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • I've updated the code above as I'd missed out a "trigger once" in the second section.

    This should work for saving your highest score, not your most recent - it's what I'm currently using in my projects without any problems.

    Do you have a capx you can share?

  • You got it, thank you very much.

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