srikanth
srikanth

Reputation: 301

how to save game state/preferences in android using libgdx

I am trying to save the values of various variables which my game is progressing, like logo number or lives available, etc using LIBGDX framework.

Code goes as such:

static Preferences prefs = Gdx.app.getPreferences("My_state");
public static void ContinuePutstate() {
    prefs.putInteger("option", MenuScreen.option);
    prefs.putInteger("lifes", Loadassets.lifes);
    prefs.putInteger("hammertouch", Loadassets.hammertouch);
    prefs.putInteger("multilogonum", Loadmultiple.multilogonum);
    prefs.putInteger("brushtouch", Loadassets.brushtouch);
    prefs.putInteger("leveluser", Loadassets.Leveluser);
    prefs.putInteger("iconnumber", CorrectScreen.iconnumber);

    System.out.println("HAd saved option "+prefs.getInteger("option")+" and original option is "+MenuScreen.option);
}

When I tried to print that, I am getting option 0 but menuscreen option actually has another value.

Upvotes: 2

Views: 4866

Answers (2)

EpicPandaForce
EpicPandaForce

Reputation: 81539

It is important to note that creating a singular static instance is the proper way to go with the LibGDX Preferences framework, because the Android OS allows you to obtain only one preferences instance, and not more. Meaning, if you tried to get more preferences than just a single one, the key-value pairs would not be saved.

Upvotes: 0

Vikalp Jain
Vikalp Jain

Reputation: 1419

after putting all values use

prefs.flush();

this will write the data to preferences

see https://code.google.com/p/libgdx/wiki/Preferences#Flushing

Upvotes: 8

Related Questions