Tutorial 3 – Saving Game State (Iphone Game Tutorial Cocos2d)

Well its day 44 of my Iphone game developing and I decided to do a third tutorial. This tutorial describes how I save the game state to the device which is an extremely important feature to include with your game (or any application for that matter).

This is actually very easy to do using NSCoder and I will describe what I have done in this 20 min video located below. Also in the video is a quick preview of what my game “Planet One” is looking like at day 44.

The video shows how I have accomplished this on my game using a singleton class. I found that using a singleton class is the best thing for my entire game, it has helped me in so many ways and now it has made my life easy for saving game state. You can still follow the video even if you are not using a singleton class as the meat of the code is always the same. BUT I would recommend to everyone – Make Yourself a Singleton Class!!

UPDATE FOR VIDEO: In the video I show the decoder (re-loading the saved data) located in my menu class, BUT after making the video I realized that this was not a good spot for decoding since the menu screen can be accessed at any time by the user. Sooo…this is an easy fix I just moved the code from that class to the

- (void) applicationDidFinishLaunching:(UIApplication*)application

in the AppDelegate.

This was a simple fix for a problem caused my my coding late late into the night. I was not paying attention (or thinking straight ) last night I guess, but all is good now.

Anyways enough talk, lets get to the video!

Tutorial 4 Video – Saving game state:

Please Leave Comments Below

About these ads

14 Responses to Tutorial 3 – Saving Game State (Iphone Game Tutorial Cocos2d)

  1. Travis says:

    Fantastic work. I’ve watched all your videos and you are an inspiration. I’ve wanted to try making a game since my college days and still have my old character sketches. Since I got married I don’t really have the time to fully dedicate to learning all the ins and outs of cocos2d. You’ve made everything 10x easier and thanks to you I can finally make it a reality.

    Thanks again and hopefully you make more tutorials in the future. Specifically with the moving platforms in this video, or perhaps just the animation changes (run, jump, attack etc) instead of just the ball. If not, then I still appreciate everything you have done and wish you all the best. =)

  2. mat says:

    Hey, just found your incredible good work. Watched you videos. I’m also working on a game. Keep going. :)

  3. mrTortex says:

    Hello.

    Wonderful tutorial, been wanting to get this working so learned about Singletons :)

    One thing, this gives me glitches on OS4 when using multitasking, when they come back to the game (if they haven’t totally closed it) it doesn’t reload the saved data. I don’t have a device to test on, just the sim. I’m saving on terminate, willbecome background etc.

    Cheers.

  4. Nirbhay says:

    Hey. thanks a lot! very well explained.

  5. Azzoor says:

    Hi,

    Great Tutorial. Is there any where i can download the project for this tutorial?

    Thanks,

    Azzoor

  6. JC says:

    Hi Paul,
    Nice to watch your tutorials. One question I had about this one was whether you could show a quick example (short code snippet would be great) of how you would update one of the stored game state variables as game is running (i.e. how to update the high score for storing when app terminates). Thanks and keep up the good work!

  7. sally says:

    Hi I’ve been following your progress on youtube. The tutorials are brilliant. Can I ask about the display you created. I take it you had a separate layer for the score etc. How did you get the layers to talk to each other, by that I mean how did a collision on one layer update the energy dots on another; and how did you organise the layers so that the HUD layer didnt scroll. many congrats on your achievements too.

  8. Stephen says:

    Hi Paul,
    I recently posted a thread on the cocos2D forum asking did anyone spot what was wrong, but I might as well ask you.

    http://www.cocos2d-iphone.org/forum/topic/13294

    I’m having issues loading the save file on iOS 4.2 after the app has been killed. As long as the app is still open in the multi-tasking, no problems. It saves and loads data fine. But if I kill the app (kill it from the multi-tasking) then try load, it won’t work, all the values are 0.

  9. zoran says:

    good stuff man! thanks for the tuts, keep them up!

  10. Gil Beyruth says:

    Hi Paul, do you have a file for this tutorial?
    Does it persist a game update? I mean if you update your game, and ppl download it again, once they install the new version, the data file will still be there for the new version to load it?

  11. Sean says:

    Thanks for the tutorial! A few questions:
    1. What kind of file is gameData.dat? Is it provided by apple or is it just like a text file or what?
    2. What kind of graphics application are you using to make the graphics for your game?

    Thanks a ton.

    • PaulsonApps says:

      Hello Sean;

      I am using Photoshop for all my image creation.

      The data file is a file that the OS can access for the app data, it probably will be similar to an encoded text file.

      • Sean says:

        Thanks for the response. How do you get the gameData.dat file into your project? I’ve never heard of the .dat suffix before. Is it just a text file that you renamed with .dat or is it a special kind of file?

  12. mintybot says:

    Thanks for the tut, but if I have one suggestion for this site I’d prefer plain old text and images instead of videos, its a lot easier on me and my sluggish internet.

    And sean, NSKeyedArchiver is saving your file to an encoded file format, perhaps XML but I am not completely sure. As for the ‘.dat’, that is just a made-up file name. We could call it anything, but .dat makes sense because you’re storing game data. It is not important what the extension is.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 58 other followers

%d bloggers like this: