Tag: retry
Adding Save Game to Battle for Vesta
by john on Mar.08, 2010, under iPhone
One of the most requested features for Battle for Vesta is the ability to save a game. This is an entirely reasonable request. Playing the game all the way through can take some time and it is inevitable that you’ll get interrupted by a phone call or some other distraction and find yourself wanting to pick up where you left off.
Also, over the last few updates the game has gotten a lot harder. I find myself getting shot much more frequently and making it through the last few missions is really difficult. Sometime I just want to replay the mission that I just died on rather than start over from scratch.
I started thinking that the retry functionality was the low-hanging fruit and that I should implement it first. Then after some thought it occurred to me that I could kill two birds with one stone. The simplest way to do this was to save the starting conditions at the start of each level and then add a new way of starting the game, a “retry” button. Pressing the retry button would start the game up on the same mission that was last played. So if you die or get interrupted you can always go back to the start of the last mission you were playing.
It turns out that this requires very little code, but it took me a bit of googling to find the bit of the iPhone framework that does what I need. It turns out that there is a persistent dictionary for the iPhone called NSUserDefaults that allows you to store key/value pairs. It also handles persisting them to flash so you don’t even have to worry about the filesystem. So now I have some simple code to save a game in my startLevel() function:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:level forKey:@"levelKey"];
[prefs setInteger:myLevels.goodRemaining forKey:@"goodRemainignKey"];
[prefs setFloat:shieldPower forKey:@"shieldKey"];
[prefs synchronize];
and some complementary code to pull that information back up when a user hits the retry button:
void retry()
{
gameOver = false;
if (isHit && deathCounter <= 0)
{
isHit = false;
uKills = 0;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSInteger retryLevel = [prefs integerForKey:@"levelKey"];
NSInteger retryGoodRemaining = [prefs integerForKey:@"goodRemainignKey"];
float retryShield = [prefs floatForKey:@"shieldKey"];
if (retryLevel == nil)
{
shieldPower = 6.0f;
level = 1;
energyCollected = 0;
}
else
{
shieldPower = retryShield;
level = (int)retryLevel;
energyCollected = 0;
myLevels.goodRemaining = (int)retryGoodRemaining;
}
startLevel();
}
}
And that's about all it took to add both save and retry to Battle for Vesta. I'll be submitting this update to the App Store today, so you'll be able to use this feature soon.
I should mention that my Googling eventually led me to the the iCode blog which had a helpful walkthrough of how to use NSUserDefaults and which convinced me that it was the correct (and simplest) way to implement what I wanted. However the blog states that the synchronize message is not needed. In my experience it is needed as I tried it without it and it didn't work.