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;

    public void create() {
        // Set up the application
            SettingsManager.writeLine("gamedata", "Launched:"+count ,FileType.LOCAL_FILE );
            SettingsManager.writeLine("gamedata", "Not First launch :"+count++ ,FileType.LOCAL_FILE );

        // Load assets before setting the screen
        // #####################################

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

What is the proper way to do this?

  • \$\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


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);
prefs.putInteger("RunCount", count);

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) {

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

then in your create() method write following

public void create(){

    LocalPreferences preferences=new LocalPreferences();

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

    //now set the count


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


You must log in to answer this question.

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