Writing your own Load / Save routines
This is my first article for GameDev.net so go easy on me. If you want to say thanks, correct sometihng, or just flame, just mail me, all feedback is good feedback (no death threats please). I’m here to (try) to explain how to make a Load/Save routine. I’ll use non-platform-specific C so people can use the code in DOS/Windows/Linux/whatever supports ANSI C. (I was thinking of also doing a C++ version of the code, but most people prefer C for pure write and read functions. If you need the code for C++, mail me and I’ll send you the C++ version.) Saving the fileI’ll try to explain the best way I can how to do a save routine in 5 easy steps. The main loop goes like this:
Ok, now let’s analyse each of these. 1. Create file and verify that it's validThis is probably the easiest thing to do, you will just have to create a file and check if it was created correctly.
Now that was easy, let's check the next one. 2. Write signature to fileI must be honest with you, this is not necessary to your save game file, but is a help for loading, I’ll explain why later, when I cover the Load routine. So to write a signature file to the game you just have to add a string to your file.
For you C genius out there I think I don’t have to explain the fwrite routine, but I’ll do it for the sake of the beginners out there. So, it is as follows, we supply a pointer to the string as first parameter (SIGNATURE points a pointer to SIGNATURE[0], now is the weird part, we must supply the size of the type of variable we are trying to write to the file, in this case char, and then, how big is the string, in this case 4 bytes (LSD + "\n" for end line). Now we just supply the pointer to the file stream and off we go. 3. Write important file descriptionNow for the hard part starts, for this part we will imagine we are making a invaders clone, so we will need some variables to old score, life, etc. We will use a struct so it can be easier to write and read from. typedef struct { char PlayerName [11]; unsigned char Lives; int xPosition; int yPosition; int Score; int BulletsLeft; int Level; } sPlayer; typedef struct { unsigned char Alive; int xPosition; int yPosition; int Speed; } sEnemy; sEnemy Enemies[ENEMIES]; sPlayer Players[PLAYERS]; In this piece of code we created two arrays, one of sPlayer and another of sEnemy, so we know we have two players and 20 enemies, this is what we will write to the file.
You already know how fwrite works so no more explaining to do. Just want to remember when passing single variables to fwrite you need to pass the address of them. After completing this step, we can say we have written the save file header ! 4. Write all necessary variablesNow for the real, real hard part (ok, not so hard but I like to glorify myself), we shall write all the data to the file. We will use fwrite so we can write the entire info of players and enemies in two simple calls.
Again we need to give fwrite the size of the variables and all it is done. 5. Close fileThis is the hardest part you need to close the file.
It was hard right? Loading the fileIf you got all the stuff this far, then this will be a singe. Loading a file is like saving it basically, let’s see the steps we need.
And another type, step-by-step. 1. Open file and verify if valid fileWe will open the file and see if it exists.
Just like for saving, but instead of writing mode, we open it in reading mode. 2. Read headerWe only have in our header three variables, below is a table explaining the all file. Name Size Description SIGNATURE 4 bytes Signature file PLAYERS OS dependet (int) Number of players ENEMIES OS dependet (int) Number of enemies Players PLAYERS * sizeof (sPlayer) Players information Enemies Enemies * sizeof (sEnemy) Enemies information Now we load the header info into some variables.
And that’s it, you have yourself the header loaded. 3. Read all variables necessaryNow this is just like the save part (again).
We use the variables we loaded (lowercase) to know how many were saved. 4. Close fileAgain
And that’s the end of our Load routine. Further Sugestions and RemarksThis part is the cooler one in my opinion, I’ll give you suggestions I think it should be cool to implement in the Load / Save Demo. EncryptionIf you don’t want that the more experienced user to pick up your saved file and edit it so he has a 100000000000 * 10 ^ 99 score then you should had encryption. It’s cool and it protects you from players that want to be number one, even if they can’t play the game. CompressionIn this article we used very small structs and variables, but imagine you have 100 floating values, about 1000 structs for objects and more information you want to save, this would create a very big, big file (I still remember Championship Manager 2 where I needed 16 Megabytes of free space just to save). To compensate this, one idea is to compress the saved file Multiple filesThis is just to simplify loading and reading routines. It’s a good idea to store all player variables in one file, world variables in another, objects in another and so on. FilenamesAllow the user to choose the filename he wants to save to, this is an obvious subject but sicne in this demo I made it hard coded, you could forget right ? Now for the regards (I hate this part). Restoring thingsRemember, after loading the file, to load all images needed, maps, and stuff, because you will probably need to load all that stuff again (it should very bad if you loaded a game in level 6 and the artwork would still be from level 1). Make a headerAlways make a header for your files, so you can load the header first, and then see what you will load (you don’t want to pass by the end of file do you ?). Enough is enough, conclusion ! Ok, it is time to say goodbye, but… "I’ll be back". Just play a little with the source code, experiment, and if you discover something cool, send it to me. Oh, not to forget, if you have ideas of articles you would like me to write, send your suggestions. Heck, send me even if you don’t have suggestions, even if it is only to say you learned something or not, I WANT feedback ! Bruno Sousa (Akura)
Discuss this article in the forums
See Also: © 1999-2011 Gamedev.net. All rights reserved. Terms of Use Privacy Policy
|