0
\$\begingroup\$

I'm writing a game using LibGDX in which I want to save the number of launches of a game in a text file. So, In the create() of my starter class, I have the following code ..but it's not working

public class MainStarter extends Game {

    private int count;

    @Override
    public void create() {
        // Set up the application
        AppSettings.setUp();
        if(SettingsManager.isFirstLaunch()){
            SettingsManager.createTextFileInLocalStorage("gamedata");
            SettingsManager.writeLine("gamedata", "Launched:"+count ,FileType.LOCAL_FILE );
        }
        else{
            SettingsManager.writeLine("gamedata", "Not First launch :"+count++ ,FileType.LOCAL_FILE );
        }

        //
        // Load assets before setting the screen
        // #####################################
        Assets.loadAll();

        // Set the tests screen
        setScreen(new MainMenuScreen(this, "Main Menu"));
    }   
}

What is the proper way to do this?

\$\endgroup\$
4
  • \$\begingroup\$ What exactly is wrong right now?? Also, you'll probably want to show code for createTextFileInLocalStorage and writeLine so that we can see how u r using libGDX. \$\endgroup\$ Commented Jun 21, 2013 at 1:42
  • \$\begingroup\$ Both the methods are working well for storing other variables.. I just want to store the number of times the game has been launched. \$\endgroup\$ Commented Jun 21, 2013 at 1:45
  • \$\begingroup\$ Shouldn't you be reading a value from that file into count when its not the first launch? Also shouldn't count be initialized to 1? \$\endgroup\$ Commented Jun 21, 2013 at 1:52
  • \$\begingroup\$ This seems like a general "how do I program" question, as opposed to anything related to game development. \$\endgroup\$ Commented Jul 11, 2013 at 12:08

2 Answers 2

1
\$\begingroup\$

I'm not sure what exactly the "SettingsManager" is, as you haven't provided any information on what it does internally. Normally for this kind of persistent data you would use Preferences to store it in a cross-platform manner. Something like the following code in your create() method:

Preferences prefs = Gdx.app.getPreferences("My Preferences");
int count = prefs.getInteger("RunCount", 0);
count++;
prefs.putInteger("RunCount", count);
prefs.flush();
\$\endgroup\$
0
\$\begingroup\$

To get the count of your game launch first make a LocalPreferences class and write following

public class LocalPreferencees{
    Preferences prefs;

    public LocalPreferencees() {
        prefs = Gdx.app.getPreferences("GAMECOUNT");         
        }

     public void setCount (int count) {
        prefs.putInteger("COUNT",count);
        prefs.flush();
    }

    public int getCount() {
        return prefs.getInteger("COUNT",0);
    }
}

then in your create() method write following

@Override
public void create(){

    LocalPreferences preferences=new LocalPreferences();

   //this will get your game launch count
    int count=preferences.getCount();

    //now set the count
    prferences.setCount(count+1);

}  

So each time when your game launches you will get the launchCount .

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .