Search code examples
javaandroidleaderboard

How to save only new Highscore and put in Leaderboard


We usually use sharedpreferences to save current data like

            SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE); 
            SharedPreferences.Editor editor = pref.edit();      
            editor.putInt("Score", gamescore);       
            editor.putInt("Level", gamelevel);  
            editor.commit();

then use this to get the data that saved in sharedpreferences

SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
gamelifes = pref.getInt("Score", 0);
gamelevel = pref.getInt("Level", 0);

but as we know that saving will overwrite each time a new score is made.

How can I only save a new high score? i.e. If the user got score under latest high score it will not be saved, only if it is a new high score. Also, how can I put this score in the Google Leaderboard?


Solution

  • I don't fully understand the code but i think you are looking to use an if statement

    highscore = pref.getInt('Highscore", 0);
    if (gamescore > highscore){
        pref.edit("Highscore, gamescore);
    }
    

    something like that