Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Tooko

Game Saving/Loading

This topic is 5280 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, I have a question. If I were to have a game that supports saving and loading, how should I go about it? At the moment I store all the object/player/projectile/terrrain etc data in seperate areas. Should I store them all in one structure like struct GAME_STATE { ... }; and then write this to a file in one chunk (fwrite) or should I save each component seperately?
Look, one of the few who hasn''t asked Salsa for an Avatar. Nor do I follow the trend of putting "I don''t follow trends" in your sig. Wait...

Share this post


Link to post
Share on other sites
Advertisement
You don''t want to save everything or the file will be huge. Create a asve packet that contains everythig essential for the state of the game but nothing more. Copy everything you need into it before you save and back out again after you load.

Share this post


Link to post
Share on other sites
here is what i do and if you are using classes then it can work for you.

i have everything about my game world in classes..EVERYTHING
there are various classes that hold all the data for my game to work.

each class has a save and restore function

the save function will save all of that classes vital information to a passed in file handle (opened in main or something) to the save file.
the restore function will load all the vital information from a passed in file handle.

so if i have like 3 classes like
CWorld, CPlayer, CEnemy

then to save the game i would do

CWorld.save(filestream);
CPlayer.save(filestream);
CEnemy.save(filestream);

and then to restore you would

CWorld.save(filestream);
CPlayer.save(filestream);
CEnemy.save(filestream);



"A soldier is a part of the 1% of the population that keeps the other 99% free" - Lt. Colonel Todd, 1/38th Infantry, Ft. Benning, GA

Share this post


Link to post
Share on other sites
quote:
Original post by ncsu121978
here is what i do and if you are using classes then it can work for you.

i have everything about my game world in classes..EVERYTHING
there are various classes that hold all the data for my game to work.

each class has a save and restore function

the save function will save all of that classes vital information to a passed in file handle (opened in main or something) to the save file.
the restore function will load all the vital information from a passed in file handle.

so if i have like 3 classes like
CWorld, CPlayer, CEnemy

then to save the game i would do

CWorld.save(filestream);
CPlayer.save(filestream);
CEnemy.save(filestream);

and then to restore you would

CWorld.save(filestream);
CPlayer.save(filestream);
CEnemy.save(filestream);



"A soldier is a part of the 1% of the population that keeps the other 99% free" - Lt. Colonel Todd, 1/38th Infantry, Ft. Benning, GA


Hmm, not a bad idea.



Look, one of the few who hasn''t asked Salsa for an Avatar.
Nor do I follow the trend of putting "I don''t follow trends" in your sig.
Wait...

Share this post


Link to post
Share on other sites
one major pain of game saving/loading is version control. make sure you implement some kind of version identifiers in your save chunks, and some checksum/markers as well. backward compatibily and reading old versions can make the code quite messy.

you can also use a text-file parsing system instead of a raw binary file system, to simplify the version control, but obviously the file size will be much bigger. then you can always convert the text files into binary files for release builds.

- my 2 cents.

Share this post


Link to post
Share on other sites
quote:
Original post by oliii
you can also use a text-file parsing system instead of a raw binary file system, to simplify the version control, but obviously the file size will be much bigger. then you can always convert the text files into binary files for release builds.
- my 2 cents.

Just a few thoughts:

I wouldn''t do that, since you will have to keep track of two
versions for your load/save code then - one binary- and one text
mode part.

I''d rather suggest to use IFF format.

You can have a header chunk that provides version information
and simply reject save games that don''t match.

Alternatively it could be a good idea to use CSV if you choose
to stick with text format first. You can easily load and modify
such files using Microsoft Excel or OpenOffice for testing/
debugging.

XML also is a good idea for text-based load/save but it requires
more work for parsing and writing.

A hybrid approach would be saving to a text buffer and compressing
the buffer to disk (decompressing to text again during load).

Cheers,
Pat

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
For extensible text file formats, use XML. If you need it in condensed binary format use WBXML (http://www.w3.org/1999/06/NOTE-wbxml-19990624/).

A note on how to load and save state ingame automatically without adding Save/Load methods to your classes. If you expose properties of each class in the form of SetX/GetX methods and give each of your classes a unique type-id, you could register your properties with a central type-management system.

So, given an object and all you know is its type-id (through a virtual method that you query or a map of object addresses to type-ids) you could write code that does this:


Object* object;
for (int i = 0; i < GetNbProperties(object); i++)
stream << GetProperty(object, i);


This code need only be written once and it will work for all your objects. You can get it to work with XML by checking for the existance of properties (version control). You can use it for automatic distribution of object state over a network. You can use it for realtime ingame editing of object properties. You can even trigger events to go off in your Set/Get methods. Whenever you add set/get methods to your type, if they get registered all this will be at your disposal automatically.

How you do this is another story entirely - there are literally hundreds of different ways to do it; each with their pros/cons. It''s just an ideal to aim for.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!