Page 1 of 2

How to save and load a game?

Posted: Fri Sep 14, 2007 7:54 am
by Weng
I would want to implement saving and loading features into my game but am not sure of how to go about implementing them.

I was thinking of creating a class which contains variables to contain all of the game's information like the player's position, whether enemies are dead or alive, number of hits the player has taken etc... and store them in a text file. Then the data in the file is loaded and used to initialise the game the next time it is loaded.

Is this the way to do it? Or are there better methods ? :)

Posted: Fri Sep 14, 2007 8:37 am
by JP
I believe you can serialise the scene (check the API for the method in scenemanager) and you can save that to a file so that you can then re-create the same scene again. Of course you'd also need other data saved as well, as you mentioned, like player health, ammo, etc. Basically any data you have in the game that you'll want again will need to be saved, which could be quite a lot.

I'm not sure how particle systems would react to this.. I know if you pause the scene and then continue the particle systems mess up (or at least they used to, not sure if that's been fixed, i think i saw something about a patch to help with that, it's a pretty trivial fix i think).

Posted: Fri Sep 14, 2007 8:45 am
by ansu832001
Hi Jp,

Can you throw some more light on how to save all the data of current scene including CustomMade Nodes and data related to it.

Best Regards.

Posted: Fri Sep 14, 2007 9:21 am
by JP
As i said, check the API:

smgr->saveScene(..)
smgr->loadScene(...)

I've never used these functions myself so i don't know anymore than what it says in the API but try those functions out and see what effect it has. As for custom scene nodes i'm not sure how it would handle those, you may have to do your own saving of those nodes and their data but that wouldn't be too hard as you'd just have to save all their important variables so they can be reloaded.

Try creating your scene, saving it and then clearing the scene and reloading it, see what stuff ends up getting saved and reloaded properly.

Posted: Fri Sep 14, 2007 10:09 am
by Umbra
if youre making an online game you can use mysql for saving everything. Thats how we do it in our project we save all object data and char data and player data to mysql.

Posted: Fri Sep 14, 2007 10:29 am
by rogerborg
I honestly wouldn't use Irrlicht's serialisation. As JP says, you'll also have to save out a lot more information anyway.

Rather, I'd decide what's actually relevent, e.g. the position, orientation and state of the relevant actors, and general game information like progress, number of ho's pimped, and so on. Save out that and just that information, using any scheme you want. If you're writing an MMORPG (God help you) then you will probably want to use a relational DB. For most games though, you can just dump a text or binary flat file with that info. Invent some simple flags and tags to identify various sections, e.g. [PLAYER], [ENEMY], [HO'S PIMPED] etc. Personally I'd use a text file during development, to ease debugging, then switch to a binary format for release. In the binary version, you can use a much simpler scheme where you might make rigid assumptions about the order in which things appear in the file, and use simple counts to indicate how many objects of type X you're saving/loading.

When loading, read in your data, using your tag scheme to identify the appopriate sections and types of data, then create your game objects from those data, and your Irrlicht objects from your game objects.

Just saving out Irrlicht objects would only get you half way there; you'd then have to associate your game data to the relevant Irrlicht objects, and you'd also be tying yourself to Irrlicht, which (much as I like it) is a Bad Idea.

Posted: Thu Sep 20, 2007 9:03 am
by ansu832001
Hi rogerborg,

Can you elaborate a bit more on that,like how to save a simple node to our own built file say XML file.I a got your point but didn't relly get how to start it.

Best Regards.

Posted: Thu Sep 20, 2007 11:29 am
by rogerborg
I wouldn't save a 'node' (in the sense of an Irrlicht node), I'd save out your game information, i.e. the position, orientation and state of all the actors in the game. If you get the position and orientation from the Irrlicht scene nodes that represent them, then fair enough, you'll need the other information as well. If you save out an Irrlicht scene, then you'll have to save out all the game state info as well, and then find some way of associating the two of them once you've loaded the scene again.

What I'm suggesting is that you treat Irrlicht as being a component of your game, rather than your game as being a component of Irrlicht.

I can't tell you how to save something out as XML, because I wouldn't do it as XML. I'd use good old fprintf() / fscanf(). Only you can decide what's significant enough in your game to save and load.

Posted: Thu Sep 20, 2007 11:49 am
by ansu832001
Hi rogerborg,
Thanks for your reply
Best Regards.

Posted: Thu Nov 22, 2007 11:06 am
by humbrol
Umbra- How do you connect to mysql via irrlicht. I am currently using dev C++ and the packages that it comes with, even thier tutorials aren't working..

Posted: Thu Nov 22, 2007 1:28 pm
by Acki
I think there is a mySQL DevPackage you'll have to install and use for this... ;)

Posted: Thu Nov 22, 2007 2:19 pm
by rogerborg
humbrol wrote:Umbra- How do you connect to mysql via irrlicht.
What's Irrlicht got to do with MySQL, or vice versa? You connect to a MySQL database the same way you would in any application, Irrlicht or otherwise. That's an "Other than Irrlicht" issue, or just Google for it.

Posted: Thu Nov 22, 2007 3:23 pm
by humbrol
per se not very much other than being useful to each other, but its a basic programming question. Ive been digging through google and the examples Ive been finding have not been working, so I was asking someone who said they had been able to do it in this thread, how they did it.

Posted: Thu Nov 22, 2007 4:32 pm
by rogerborg
Fair enough, and good luck with it. However, as he hasn't posted anything in over 6 weeks, it might be more productive to start a new topic and describe exactly what your problems are (beyond "not working"). I'm sure someone here can help.

Posted: Thu Nov 22, 2007 9:00 pm
by radubolovan
Another posibility is to create a ByteArray class and implement methods like WriteByte, WriteInt, WriteFloat, WriteString, etc and ReadByte, ReadInt, ReadFloat, ReadString. And then a SaveManager class who has a ByteArray prroperty and saves/loads to/from a file anything you want.
It's tricky, but also you can do this in another thread and it can be done with streaming :)