what is the most used method or algorithm for saving game state (profiles), databases text files how there encryption goes and things related.
I have seen Caesar IV used mySQL.
any suggestions.
what is the most used method or algorithm for saving game state (profiles), databases text files how there encryption goes and things related.
I have seen Caesar IV used mySQL.
any suggestions.
I'm pretty sure it is most often done in a proprietary (binary) format, just writing each variable from whatever game state struct you use into a file, or reading that file back into an instance of the struct. In C++ for example you can treat an object like an array of bytes and just fwrite() it into a file, or fread() from the file and cast it back into its object form.
If you are using Java or another language you can opt to use serialization to make the task really easy. The Java explanation is at the bottom of this answer; other languages (higher-level than C++) surely have their own serialization libraries or built-in functions as well, which you should opt to use. No matter how inefficient the serialization routine might be, hard drive space nowadays is cheap, especially when the game state shouldn't be very large at all, and it prevents you from writing and maintaining your own serialization routines.
You should absolutely not use MySQL (just read the first sentence of this review). If you really need a relational database for some reason, use SQLite; it is a lightweight database system and exists in a single database file. But for most games, relational databases are not the way to go, and companies which try to use them usually end up using them as a key-value lookup table rather than a true relational database.
Any sort of encryption of local disk files is only obfuscation; any hacker will just sniff the data right after your program decrypts it. I'm personally against that sort of thing, ESPECIALLY with one-player games. My view is that the owners of the game (paying customers, mind you) should be allowed to hack at the game if they want to. In some cases it can create a greater sense of community, and "mods" can be developed for your game which drive more customers to you. The most recent example that comes to mind is the Portal in Minecraft mod that was recently released. It's published in gamer news sites all over the internet, and you can bet it drove up sales of Minecraft.
If for some reason you are actually crazy enough to be using Java for game development like I am, here's a quick explanation of serialization in Java:
Keep all of your game state data in one class, make the class serializable by implementing
Serializable
, use thetransient
keyword if you mix special instantiated variables into the class (transient objects are not serialized), and simply useObjectOutputStream
to write to file andObjectInputStream
to read from file. That's it.
If you are writing your own code, say in C++, you can use binary files. Binary files give you a form of encryption through obfuscation. But as documented all over the internets, that is a very weak form of security.
OR, you can use something like RapidXML if you want a human readable solution.
If you are usuing some kind of framework, look into it's file support functions. HTH
If you can use the same format that you use to store your levels in, it is a bonus.
For example, a game like Peggle might have an initial board layout, number of balls, current score (0 for the initial board), etc. Then a saved game can use exactly the same format.
If you use the same format then the save game and the level load can share code making your job easier!
For a game with really big map files, the save game can include the map file by reference. The initial level files might do the same thing, even, which makes coming back to that map easier too if for some reason the story has the character come back to the same place, except some of the NPCs are dead and there's dead bodies lying all over.
Most games I've seen just use hand-written code to read/write from binary files. Often, the on disc format will be an exact replica of the memory layout of an object so loading is just:
It's fast, but it's a chore to maintain, platform-specific, and inflexible.
Lately, I've seen a couple of games use SQLite. People I've talked to seem to like it.
Serialization is mentioned in some of the other answers, and I agree that is a reasonable solution. One way it fails though is in versioning.
Say you release your game and people play it and create some save games. Then in a later patch you want to fix a bug or add some features. If you use a simple binary serialization method and you add a member to your classes your serialization may not be compatible with the old save games when the customers install your patch.
So whatever approach you use, make sure you think of this issue before you release the game the first time! Customers will not be happy if they have to start over after applying a patch.
One way to avoid this is to have each basic data type implement Save(version) and Load(version) methods that are smart enough to know what data to save and load for each version of the game. That way you can support backward compatibility of your save games and fail gracefully if a user tries to load a save game from a newer version of the game than they are running.
[field: NonSerialized]
– Mike Strobel Aug 20 '10 at 18:33