title: What is the best way for me to Save & Load gamestate
body: <p>I was just wondering, is it hard to save / load gamestate?</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>What do you recommend?</p>
  
  <p>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.</p>
  
  <pre><code>//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:documentPath]; 
      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;
  }
  </code></pre>