How to save and load a game?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Weng
Posts: 97
Joined: Tue Oct 03, 2006 4:23 pm
Location: Singapore

How to save and load a game?

Post 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 ? :)
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post 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).
Image Image Image
ansu832001
Posts: 95
Joined: Thu Mar 01, 2007 6:39 am

Post 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.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post 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.
Image Image Image
Umbra
Posts: 19
Joined: Sun Aug 26, 2007 12:54 pm

Post 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.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post 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.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
ansu832001
Posts: 95
Joined: Thu Mar 01, 2007 6:39 am

Post 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.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post 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.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
ansu832001
Posts: 95
Joined: Thu Mar 01, 2007 6:39 am

Post by ansu832001 »

Hi rogerborg,
Thanks for your reply
Best Regards.
humbrol
Posts: 83
Joined: Sun Nov 18, 2007 8:22 pm

Post 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..
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

I think there is a mySQL DevPackage you'll have to install and use for this... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post 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.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
humbrol
Posts: 83
Joined: Sun Nov 18, 2007 8:22 pm

Post 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.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post 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.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
radubolovan
Posts: 60
Joined: Tue Nov 13, 2007 7:03 pm
Location: Bucharest - Romania
Contact:

Post 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 :)
Post Reply