Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-10-2011, 11:41 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 80
Chris1979 is on a distinguished road
Default Question about Save gamestate

Hey Guys!

this might be a stupid question but I need to ask it anyway

I´ve just used the applicationWillTerminate method to save my gamestate.

Do I need the other methods as well or is it ok to only use the applicationWillTerminate method for this?

thanks in advance!

The code below show how I save the gamestate in the applicationWillTerminate method

Code:
//Set up the game state path to the data file that the game state will be saved too
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	NSString *gameStatePath = [documentsDirectory stringByAppendingPathComponent: @"gameState.dat"];
	
	//Set up the encoded and storage for the game state data
	NSMutableData *gameData;
	NSKeyedArchiver *encoder;
	gameData = [NSMutableData data];
	encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData: gameData];
	
	//Archive our object
	[encoder encodeInteger: [[GameManager sharedGameManager] level]forKey:@"Level"];
	[encoder encodeInteger: [[GameManager sharedGameManager] HScore]forKey:@"HighScore"];
	[encoder encodeInteger: [[GameManager sharedGameManager] PScore]forKey:@"PlayerScore"];
	[encoder encodeInteger: [[GameManager sharedGameManager] currenttime]forKey:@"CurrentTime"];
	[encoder encodeBool: [[GameManager sharedGameManager] isThereASavedGame]forKey:@"isThereASavedGame"];
	[encoder encodeBool: [[GameManager sharedGameManager] isMusicON]forKey:@"isMusicOn"];
	[encoder encodeBool: [[GameManager sharedGameManager] isSoundEffectsON]forKey:@"isSoundEffectsON"];
	
	//Finish encoding and write to the gameState.dat file
	[encoder finishEncoding];
	[gameData writeToFile:gameStatePath atomically:YES];
	[encoder release];
Chris1979 is offline   Reply With Quote
Old 11-12-2011, 02:52 AM   #2 (permalink)
almostfunnydev
iPhone Dev SDK Supporter
 
rocotilos's Avatar
 
Join Date: Oct 2009
Age: 34
Posts: 3,015
rocotilos is on a distinguished road
Default

in OS4, we have multitasking apps. so, u need to save it when application is moving to the background state as well. (ie, pause the game then save data)
rocotilos is offline   Reply With Quote
Old 11-12-2011, 08:09 AM   #3 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 80
Chris1979 is on a distinguished road
Default

Quote:
Originally Posted by rocotilos View Post
in OS4, we have multitasking apps. so, u need to save it when application is moving to the background state as well. (ie, pause the game then save data)
Hey!

thanks for the reply

I'm guessing that I need to call these

Code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	NSString *gameStatePath = [documentsDirectory stringByAppendingPathComponent: @"gameState.dat"];
	
	//Set up the encoded and storage for the game state data
	NSMutableData *gameData;
	NSKeyedArchiver *encoder;
	gameData = [NSMutableData data];
	encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData: gameData];
from somewhere else then
Chris1979 is offline   Reply With Quote
Old 11-21-2011, 09:25 AM   #4 (permalink)
almostfunnydev
iPhone Dev SDK Supporter
 
rocotilos's Avatar
 
Join Date: Oct 2009
Age: 34
Posts: 3,015
rocotilos is on a distinguished road
Default

The whole saving code must also go into these:

Code:
- (void)applicationWillResignActive:(UIApplication *)application {
// called when app is interrupted by phone call, and so on.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
// when user press Home button.
}
rocotilos is offline   Reply With Quote
Old 11-21-2011, 11:25 AM   #5 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 80
Chris1979 is on a distinguished road
Default

Quote:
Originally Posted by rocotilos View Post
The whole saving code must also go into these:

Code:
- (void)applicationWillResignActive:(UIApplication *)application {
// called when app is interrupted by phone call, and so on.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
// when user press Home button.
}
I see...

so it's only in the applicationWillTerminate, applicationWillResignActive and the applicationDidEnterBackground methods that I need to put the code?
Chris1979 is offline   Reply With Quote
Old 11-22-2011, 02:34 AM   #6 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 1
Velazquez is on a distinguished road
Default re

it is a stupid question
Velazquez is offline   Reply With Quote
Old 11-22-2011, 08:27 AM   #7 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 80
Chris1979 is on a distinguished road
Default

Quote:
Originally Posted by Velazquez View Post
it is a stupid question
maybe for those who already know this stuff.
Chris1979 is offline   Reply With Quote
Old 11-23-2011, 04:49 AM   #8 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: Helsinki
Posts: 304
fiftysixty is on a distinguished road
Default

Quote:
Originally Posted by Chris1979 View Post
I see...

so it's only in the applicationWillTerminate, applicationWillResignActive and the applicationDidEnterBackground methods that I need to put the code?
I believe that technically you shouldn't need to do any state saving at applicationWillResignActive since from that point on you either return to your app and continue as it was, or transition into another app in which case either your app will terminate or move to the background. You should make sure your app is paused at applicationWillResignActive.

Also, a point to remember: do your loading in the right method, which is applicationDidFinishLaunching:withOptions. Some app templates still use the old applicationDidFinishLaunching without the options, and that can lead to problems in some situations. Also remember that if your loading takes a lot of time, do it asynchronously because applicationDidFinishLaunching:withOptions is expected to return in about 5 seconds. After that, the system will kill your app as unresponsive.
fiftysixty is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 375
10 members and 365 guests
apatsufas, comicool, Creativ, Dalia, dansparrow, LunarMoon, mer10, Murphy, pbart, Tomsky
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,127
Posts: 402,916
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:01 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0