Question

Im making a game in Libgdx, and I'm trying to implement a highscore. My thought process was to just make a file with the highscore, read the file, and if the player's current score is higher than the saved highscore, override the previous highscore and write the current one to the file. Then, I would read the file to display the highscore.
When I write to the file and read from it while in the same session it's fine, but when I close out the application and open the highscore text file, the highscore is missing. So basically, when I write to the file and close out the program, the file doesn't save whatever I wrote to it. Here's the code-

FileHandle highScore;
highScore = Gdx.files.external("data/highscore.txt");
    OutputStream out = null;
    out = highScore.write(false);
    try {
        out.write(Integer.toString(score).getBytes());
        Gdx.app.log(" score test", highScore.readString());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Help would be much appreciated. Thanks!

Was it helpful?

Solution

read this link and use shared preference for saving highscore .

and do remember to use

prefs.flush();

else your data will be lost on devices.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top