How do I make highscore with localstorage ?

0 favourites
  • 3 posts
  • How do I make highscore with localstorage ?

    why my highscore always NaN..

  • Try Construct 3

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

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

  • shuwand

    [quote:2fvr1we3]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

    [quote:2fvr1we3]why my highscore always NaN..

    Use in() to turn the values returned from local storage into numbers, i.e. int(LocalStorage.ItemValue)

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