Saving game states etc.

Is there a simple way to save game states/data on the iPhone - For instance if I want to start a game at a specific level after it has been restarted or save scores locally?

/Patrik

cocoa does have an object for this that you can load your variables into and get them back when the application starts up, but since you’re using c++ likely that wouldnt be convenient.

for myself, i just use http://www.stfj.net/iPhone/ofxiPhoneFile-001.zip to write and read data to the phone. the idea would be to read in data when the application starts up if if that data exists, otherwise to write a default file. and then have some code in the exit() method in testApp.cpp that writes out the data you need to recover the gamestate to the file

cocoa makes it really simple:
to load an int, you add for example in setup()

  
  
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];  
testInt = [defaults integerForKey:@"testInt"];  
  

testInt is defined in .h file

to save it, you add, for example in touchDown():

  
  
testInt++;  
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];  
[defaults setInteger:testInt forKey:@"testInt"];  
  

Both keys most be the same, but don’t have to be the name of the integer!

  
  
Floats:  
[defaults floatForKey:@"testFloat"];  
[defaults setFloat:testFloat forKey:@"testFloat"];  
  
Bools  
[defaults boolForKey:@"testBool"];  
[defaults setBool:testBool forKey:@"testBool"];  
  

By the way: You have to rename testApp.cpp to testApp.mm, because you are mixing obj-c with c++ here.

Regards,
Gestalt

touché!

thats awesome

Thanks guys, just what I was looking for.

/P

Hi

I am using the cocoa code for an iphone app and it saves my values nicely. but only if i quit the app via the home button.

if i quit the app with:

std::exit(0)

or

[[UIApplication sharedApplication] terminateWithSuccess];

the values are not saved, even though the saving happens during touchUp.

any ideas?

thanks,
stephan.

ok i think i found a solution:

after saving the values add [[NSUserDefaults standardUserDefaults] synchronize];

like this

  
  
testInt++;  
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];  
[defaults setInteger:testInt forKey:@"testInt"];  
[[NSUserDefaults standardUserDefaults] synchronize];  
  

so when i now quit the app with a exit button/ command the values are being remembered.

cheers,
stephan.

Hi, I hope I’m not too late but… does anyone have an example of saving to a text file in the ios documents folder? I want to be able to see the created file.

Thanks.

trentbrooks, the XMLSettings example should be sufficient for showing how to save to a text file in the documents folder.

Thanks Seth, i dug through the xmlsettings and found what I needed but finally settled on this instead:

// create or open a file
string filename = “myfile.txt”;
File pFile = fopen((ofxiPhoneGetDocumentsDirectory() + filename).c_str(),“w”);

// add some lines of text
string line = “saving some data \n”;
fputs (line.c_str(),pFile);

// save file
fclose (pFile);