开发者

What is the best way for me to Save & Load gamestate

开发者 https://www.devze.com 2023-03-08 06:36 出处:网络
I was just wondering, is it hard to save / load gamestate? I\'m creating a game and I want to be able to save the gamestate in case there\'s a call or the player ends the game to continue to play ano

I was just wondering, is it hard to save / load gamestate?

I'm creating a game and I want to be able to save the gamestate in case there's a call or the player ends the game to continue to play another time or the game crashes, then give the choice to start a new game or continue where he/she left off.

The gamestate contains SFX, background music (the background music should continue where it ended). (buttons to both), a grid full of uiimages (the images should reappear at the same spot they where before the game ended), score (both highscore and playerscore), level and 3 more uiimages outside the grid.

What do you recommend?

Are there any good tutorials out there? I've checked but I only seem to find for cocos2d and I'm not using cocos2d at the moment.

//Load the saved gamestate
- (void)loadGameState {
    // Set up the file manager and documents path
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; NSMutableData *gameData;
    NSKeyedUnarchiver *decoder;
    NSString *documentPath = [documentsDirectory stringByAppendingPathComponent:@"gameState.dat"];
    gameData = [NSData dataWithContentsOfFile:documentPa开发者_运维问答th]; 
    decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:gameData];

    NSLog(@"INFO - GameScene: Loading saved game duration."); 
    //timeSinceGameStarted = [[decoder decodeObjectForKey:@"timeSinceGameStarted"] floatValue];
    NSLog(@"INGO - GameScene: Loading saved game score."); 
    playerscore = [[decoder decodeObjectForKey:@"playerscore"] floatValue];
    NSLog(@"INGO - GameScene: Loading saved high score."); 
    highscore = [[decoder decodeObjectForKey:@"highscore"] floatValue];
    NSLog(@"INGO - GameScene: Loading saved level."); 
    level = [[decoder decodeObjectForKey:@"Level"] floatValue];
    NSLog(@"INFO - GameScene: Loading game time data."); 

    [decoder release];
}

//Saving the current gamestate
-(void)saveGameState {
    NSLog(@"Saving game state");

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *gameStatePath = [documentsDirectory stringByAppendingPathComponent:@"gameState.dat"];

    NSMutableData *gameData; 
    NSKeyedArchiver *encoder; 
    gameData = [NSMutableData data]; 
    encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:gameData];

    // Archive the games timer settings
    NSNumber *savedGameStartTime = [NSNumber numberWithFloat:totalSeconds]; 
    //NSNumber *savedTimeSinceGameStarted = [NSNumber numberWithFloat:timeSinceGameStarted]; 
    NSNumber *savedPlayerScore = [NSNumber numberWithFloat:playerscore];
    NSNumber *savedHighScore = [NSNumber numberWithFloat:highscore];
    NSNumber *savedcurrentlevel = [NSNumber numberWithFloat:level];

    [encoder encodeObject:savedGameStartTime forKey:@"gameStartTime"];
    //[encoder encodeObject:savedTimeSinceGameStarted forKey:@"timeSinceGameStarted"];

    [encoder encodeObject:savedPlayerScore forKey:@"playerscore"]; 
    [encoder encodeObject:savedHighScore forKey:@"highscore"]; 

    [encoder encodeObject:savedcurrentlevel forKey:@"Level"]; 

    // Finish encoding and write the contents of gameData to file
    [encoder finishEncoding]; 
    [gameData writeToFile:gameStatePath atomically:YES]; 
    [encoder release];

    // Tell the game controller that a resumed game is available
    //sharedGameController.resumedGameAvailable = YES;
}


The one method that comes to my mind would be to store all of this information in a plist or similar; the coordinates and positions of things, the time of the music track etc, and then when the user is presented with the option to resume their last session, you can just read the contents of said plist and put everything back where it was.

I understand this is a particularly crude answer, but this methodology has worked in the 2 & 3D apps I have worked on in the past - admittedly we were only tracking a few dozen items - I am sure your game is likely to be much larger.

In response to your comment:

To accomplish this, I used NSUserDefaults - so you will find some good information by looking at the class reference on the Apple site. Create a NSUserDefaults member in a relevant place which can then poll your graphics' instances and music classes etc for how far along they are - so you'll need to write a method that returns this information for each class that you want to save - or: for each frame, get the respective classes to automatically write their latest positions etc to your defaults file. To setup the defaults file...

yourNSUserDefaultsMember = [NSUserDefaults standardUserDefaults];
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithContentsOfFile: [[NSBundle mainBundle]pathForResource: @"Defaults" ofType: @"plist"]]];

In order for this to work, you need to create a plist file called Defaults or whatever you like. Then using the method calls located in the class reference, you can easily write strings and ints etc to it.

0

精彩评论

暂无评论...
验证码 换一张
取 消