Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
    
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. –  XiaoChuan Yu Jun 21 '13 at 1:42
    
Both the methods are working well for storing other variables.. I just want to store the number of times the game has been launched. –  Vishal Kumar Jun 21 '13 at 1:45
    
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? –  XiaoChuan Yu Jun 21 '13 at 1:52
    
This seems like a general "how do I program" question, as opposed to anything related to game development. –  Trevor Powell Jul 11 '13 at 12:08

2 Answers 2

up vote 1 down vote accepted

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();
share|improve this answer

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 .

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.