SQL save game

Started by
2 comments, last by ApochPiQ 11 years ago

Hi all,

I am currently using a sqlite database file that I load up, access and modify during the process of the game running, which I realize is just not going to work for me overall. I've currently got to the point where I'm doing modifications to the database that's just not going to allow me to refresh this for a new-game, or really support the idea of being able to change the in-game database for updates etc, whilst still allowing the saved games to not be destoryed. wacko.png

The idea I've thought of to get around this issue is to still load the database up from a file, but instead to copy the contents of this (or attach? I'm not sure) into an "in-memory" database which the game will use during execution. Then, when I need to save the game, I was thinking of doing a comparion between the in-memory database and the stored one on-disk, fashioning a shiny new "saved game" database which just stores the changes between the two. In this way, upon loading up a saved game, it would load up the "template" database from the disk as normal (into the "in-memory" database) and then basically "patch-in" the changes from the saved game into this too.

My question though, is what would be the easiest/best way to actually detect the differences between two databases like this so that I am able to create a save-file with only the changes? (and be able to load it again in the same way).

Just to note: I'm not the world's greatest expert with SQL, so please be patient in explaining anything etc. Also, I'm completely willing to any suggestions if you think I've gone crazy and this just isn't possible or a good idea to begin with!

Thanks in advance for any support on this! smile.png

Advertisement
My recommendation is to do what is the easiest. That save file comparison seems complicated and would likely be buggy. Is there any reason you are using SQL?

What has worked really well for me in doing save files is JSON. It will let you expand your save files between updates with no problem and you can simply use a separate file per save game. When saving the game you simply rebuild the entire file from the game state rather then trying to determine what is different. Doing a complete rebuild may sound inefficient but it should be fast enough for any reasonably sized game.

There are libraries for JSON in almost any language.

If you are set on SQL, or it is too entrenched in your project for you to want to scrape it out then I would recommend adding a column in each table. The column would be an integer specifying the save file id. Then you could read and modify the tables with that added parameter to only effect the save file of interest. And you in memory database is a good suggestion.

Another thing you could do is have a database checker that runs when you game starts. What you would do there is have the checker determine what format the tables are in. If they are outdated then have it convert the database format. That way once your game has be started it can assume the database is in the correct format.
My current game project Platform RPG

I understand where you are going with your question, but I think its a bit over-done. Unless your are developing a Civilization like strategy game or a game with thousands of automatic entities, its just not going to be efficient. In general, save games should only be called when loading or saving games. Once the data from the save game file is copied to the "standard" game memory, the save game file is closed. During saving, if you want to overwrite an existing save game, you simply clear the contents of the save game file and copy all the data from memory to the file.

What I just described above is essentially what you described and has been used to years. In your design though you talk about using a template database and "patch" database. Its a good concept, but having to load two files and copy data twice is going to take much long compared to a single file.

I recommend you re-think your save game concept. Your idea isn't bad, its just that SQL is designed for large datasets, not small game saves. Possible replacements that are easily integratable are JSON (as mentioned by HappyCoder) and XML. My prefered method is using XML but compressing and encoding it with a passphrase in binary. With my current system, I'm able to load 1000 separate game entities (equal to approx 500KB) into a structure in memory in about 10-15 seconds or so.

Some favourite quotes:Never trust a computer you can't throw out a window.
- Steve Wozniak

The best way to prepare [to be a programmer] is to write programs, and to study great programs that other people have written.
- Bill Gates

There's always one more bug.
- Lubarsky's Law of Cybernetic Entomology

Think? Why think! We have computers to do that for us.
- Jean Rostand

Treat your password like your toothbrush. Don't let anybody else use it, and get a new one every six months.
- Clifford Stoll

To err is human - and to blame it on a computer is even more so.
- Robert Orben

Computing is not about computers any more. It is about living.
- Nicholas Negroponte

SQL is the wrong tool for savegame data. You want a state dump, not a relational database. Don't try to shoehorn state data into a relational model, it's pointless.

Just write a gamestate serialization system like everyone else ;-)

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement