Persistent High Score Saves On Mobile

From the Asset Store
An addon collection that provides native mobile features for C3 and C2 game application projects.
  • Hello there.

    I'm trying to permanently save a high score for a mobile game. Everything is working fine on the computer; even closing completely and reopening retains my high score. Unfortunately, when I test on the phone with remote preview my high score is lost when I close the game and reopen.

    Would remote preview on mobile not reflect a functional save to local storage? Do I need to fully export the project to have this register? Not sure what to try from here especially with everything performing properly on the computer.  I'm using the global variable saved to local storage method and testing on an iPhone 11.

    First I made a global variable HighScore = 0 and an instance variable for my Player object called Score which is reset to 0 every retry on start of layout. (I know Score could have been a global variable as well, but I don't think that's the issue here)

     

    - On Start of Layout: Check item "HighScore" exists

    - On item "HighScore" exists: Set HighScore to LocalStorage.ItemValue

                 Set text to "High Score :" & HighScore

    - On item "HighScore" missing: Set item "HighScore" to 0 (to set initial value if players first play-through)

    Then when player loses last life I compare their current run Score with the global HighScore variable that was set above. If the player just beat the HighScore then I take the new best score and set it as the new value for HighScore. This is also where I overwrite the "HighScore" in Local Storage with set item.

    - Player.Score > HighScore: Set HighScore to Player.Score

    Set item "HighScore" to HighScore (LocalStorage save)

      Set text to "High Score :" & HighScore

    Not sure what I'm missing here. Any advice would be greatly appreciated. If you need any additional information please let me know.

      

    Thanks guys.

  • I think it's because every time when you start a new remote preview session, you get a new link. Try to reload the game on mobile without closing the preview dialog in C3. See if in this case high score will be retrieved successfully from LocalStorage.

  • Hey dop2000. Thank you for the quick response. I gave that a shot, but I'm still experiencing the same issue. Even with a reload of the same link the high score is lost.

    Could it be something with my phone or is that crazy talk? I'm sure it's a code error on my part, but with the events only spanning a few blocks and it remaining functional on the computer I'm not sure how to approach it next.

    I read that because retrieving values from local storage is not an immediate action if the device is slow it may not have the value accessible at the moment I'm trying to display it.

    I would be surprised if this is the problem though because I call for it on the start of the layout and even if the player tries to get game over as quickly as possible it would take a few seconds. I only display the high score when the Game Over layer is visible so I feel like that would be a sufficient amount of time.

    Anyways thanks again for the help. If you have any more thoughts or need any more info please let me know.

  • Is it possible that you are saving and loading highscore at the same time? If at the end of the level you save the highscore, and immediately restart the layout or move to another layout, where you are trying to read from Local Storage - this may cause problems on slower devices like mobiles. Ideally, you should read from LocalStorage only once when the app starts, I prefer doing this in the loader layout.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Hey sorry for the late response, I haven't had a chance to work on this for a while.

    On some testing attempts I did quit back to the Start Menu after setting a high score. The Start Menu was my first layout and also where I made the read from Local Storage.

    This could definitely have triggered the problem you suggested of saving and then immediately trying to load. I would also be making unnecessary checks from Local Storage every time the player goes back to Start Menu.

    On other attempts though I just closed from the main game after the Game Over layer was visible and the "HighScore" item set (if Player.Score > HighScore). That shouldn't cause the overlapping save/load issue, but maybe I exited too soon and the new "HighScore" item was never set successfully.

    I created a new layout ("Loader Layout"), set it to First Layout, and then checked the use loader layout box. I moved the original Local Storage load block (check item exists, set global variable HighScore to LocalStorage.ItemValue, item "HighScore" missing set item "HighScore" to 0) to the Loader Layout event sheet.

    This would make loads from Local Storage only happen once when the game starts and not get triggered by every quit to Start Menu. It should also prevent potentially attempting to load when it's already trying to save.

    The new load up worked on the computer (I even cleared Local Storage to start from scratch), but it still didn't retain the high score on mobile. I began to question whether the saves to Local Storage were happening at all with the phone so I further adjusted the game over events.

    Now, when the game over conditions are met and the player sets a new high score, I don't even show the game over layer until On item "HighScore" set is complete. The layer actually does become visible indicating a successful save.

    I also edited the Loader event sheet so that the player cannot advance to the Start Menu until either:

    - On item "HighScore" exists is complete and the HighScore global var is set to LocalStorage.ItemValue

    OR

    - if item "HighScore" is missing and set to 0, On item "HighScore" set complete.

    Even with these extra measures to prevent interrupting saving and loading, the high score is still lost from Local Storage on mobile remote preview when closing and reopening (still with same link).

    I thought these last couple of tweaks would have cracked it for sure, but I'm still managing to screw this up somehow. Maybe I did the Loader layout wrong, but that seemed pretty straightforward and the new set up still works perfectly on the computer.

    Anyways thanks again for all the help, I really appreciate it. If you have any other ideas or need any more info please let me know.

  • Here is what I usually do in my projects -

    in Loader layout: On start of layout -> Check if Local Storage item exists -> Wait 1 second -> Go to Main layout

    One second should be enough to read data from LS, and I never read it again in other parts of the game.

  • Hi All,

    I'm facing the same issue -- just for my clarification dop2000 were you able to get this working on the remote mobile preview? If I preview my project on desktop/laptop I'm not having any issue -- it is only when I try the remote preview and am testing on mobile where I can't seem to get this to display correctly.

    Thanks!

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