iOS and persisting game state

Coders forum. Discuss techniques, swap code snippets, ask for help etc. relating to all forms of software development.
Post Reply
User avatar
sack
Baaaaaaa!
Baaaaaaa!
Posts: 7077
Joined: Fri May 10, 2002 4:13 pm 0
XBL Gamertag: evilgopher
PSN: sixFiveOhTwo
Location: Rijswijk, The Netherlands
Contact:

iOS and persisting game state

Post by sack »

I've got around a couple of K of game data, mostly top scores per level, which levels are unlocked, etc. Stuff that is only updated at the end of play and it would be best to keep saved across play sessions.

What is the best way of doing this? I'm sure you can just read/write a file in the usual way, but that would then be inside your package. Am I right in assuming if I were to push out an update the existing package would just get nuked and I would then lose those files?

If so, what is the recommended alternative? I just want to dump a couple of K of binary blob someplace where it will persist.
SiN
Baaaa!
Baaaa!
Posts: 850
Joined: Sat Dec 11, 2004 4:25 am 0
Location: Canada
Contact:

Re: iOS and persisting game state

Post by SiN »

The Documents folder of your app persists between updates. Here's what I use:
NSString* fullPathFilename(NSString* file)
{
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* result = [[[NSString alloc] initWithFormat:@"%@/%@", [paths objectAtIndex:0], file] autorelease];
return result;
}
32x32 . duotrix . geomex . smiley's shooty adventure . by caffeine monster software | my blog
User avatar
Oaf
Baaaaa!
Baaaaa!
Posts: 1688
Joined: Mon Nov 11, 2002 1:14 pm 0
Location: Dark Hedges
Contact:

Re: iOS and persisting game state

Post by Oaf »

User avatar
tufty
Sheepie Whore
Sheepie Whore
Posts: 12525
Joined: Fri Jan 07, 2005 4:52 pm 0
Location: French Alps

Re: iOS and persisting game state

Post by tufty »

As has been said, use an SQLite database or property list in 'Documents', or use NSUserDefaults.

*never*, *ever*, *ever*, try to store data in your application bundle. If you try to do that, the ghost of Steve Jobs will come round and break your knees.
"massive retard™", "full of shit™", "tiny bitch™", "retarted™", "a bit of a one trick pony™", "petty™", "simple minded idealistic twat™", "miserable humourless fucker™"
User avatar
pocketfun
Baaa!
Baaa!
Posts: 214
Joined: Sat Feb 07, 2004 11:23 am 0

Re: iOS and persisting game state

Post by pocketfun »

NSUserDefaults if the data is small is the right way to do this. Also on ios5 you will almost get iCloud support for free by doing that.
User avatar
korruptor
Baaaaaa!
Baaaaaa!
Posts: 4291
Joined: Sun Sep 22, 2002 10:35 am 0
Location: Under The Northern Lights
Contact:

Re: iOS and persisting game state

Post by korruptor »

You can put quite large lumps of data in NSUserDefaults. That's what I use as well.
"Don't go into a trance boys..." :: DJ Mixes
melkig
Posts: 9
Joined: Fri Oct 31, 2008 11:06 am 0

Re: iOS and persisting game state

Post by melkig »

Be careful storing too much data in NSDocumentDirectory. All that now goes to the iCloud and if there's too much in there, the nice people at Apple with reject your App unceremoniously.

Small amounts they are ok with....

We were told to use NSCacheDirectory instead for big saves
User avatar
sack
Baaaaaaa!
Baaaaaaa!
Posts: 7077
Joined: Fri May 10, 2002 4:13 pm 0
XBL Gamertag: evilgopher
PSN: sixFiveOhTwo
Location: Rijswijk, The Netherlands
Contact:

Re: iOS and persisting game state

Post by sack »

Well I'm thinking of less than 1K. Size-wise it's nothing, but given that small file represents all of your progress made so far you'd miss it :)

I'd rather write a file to an acceptable folder to be honest, because I'm trying to keep as much of the code common between iOS/android/windows as possible.
Post Reply