Archived

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

DigitalChaos

Best way to save game

Recommended Posts

I was wondering what the best way to save a game would be. Here''s my example. A wrestling sim. At the beginning, everything has there default settings and attributes, etc. But as you play, all these things change according to what u do. So they aren''t default anymore. What would be the best way to save the game so the player can load it again later and resume his game? I thought about having all the data stored into a text file and then loading it into memory, but i don''t want the player to be able to cheat and change the information in the text file. So, anyone have any good ideas on how to save the game?

Share this post


Link to post
Share on other sites
I know a lot of people (including myself) that when working with c++ tend to put all variables that should be saved into a class and then just writing the entire instance of the class to/from the file.

It''s about the same as doing what sliderag said with all of your variables, except it is easier to manage when there is only the one thing to read/write.



Drakonite

[Insert Witty Signature Here]

Share this post


Link to post
Share on other sites
That would work for a game where the number of game objects is static and the environment doesn't change.

In a game where you are in a particular mission, have retrieved a few of the keys, unlocked some of the doors, dispatched some of the enemies, and have dropped some looted items on the ground, saving it gets a little more complicated.

[edited by - Waverider on November 13, 2003 8:53:26 PM]

Share this post


Link to post
Share on other sites
I always wondered how the Halflife save game mechanism works...since you can go back to any level at any time (with alot of patience...) they would have to save alot of stuff like killed enemies, picked up ammo, etc. Is it just brute force? What other ways are there...?

Share this post


Link to post
Share on other sites
Only way I know of is assume that the game will load the default mission first, then use your saved game to update existing object states (enemies, doors, switches, etc.), and add the new objects. I imagine each object with a state has its own unique ID so that the saved game updates the correct ones when it is loaded, and adds the new ones for the saved game's object ID's that don't already exist.


[edited by - Waverider on November 13, 2003 9:07:11 PM]

Share this post


Link to post
Share on other sites
It''s not really that much more complicated to save a game in which there is a variable environment. For example, linked lists with global scope can be used to keep track of enemies, items, etc., just so long as the lists are constantly updated whenever a change occurs.

Then, when it comes time to save the game, the programmer just has to iterate through the lists and write out the states of everything contained therein.

As far as the Half-Life save game functionality goes, I''m not sure how it works, but perhaps the state of the entire world is recorded at once, perhaps in separate files, whenever the game is saved. I suppose this would be a brute-force method, though. Maybe to do it elegantly, certain assumptions can be made; for example, after the player leaves a level, all the enemies are assumed defeated, therefore removing the requirement of them being saved to disk, leaving only the level geometry to be loaded.


"Skepticism.... that great rot of the intellect." - V.H.
Bah, what does HE know?


Albekerky Software

Share this post


Link to post
Share on other sites
OK, so I write all variables that need to be saved to one class, but what if there are 60 wrestlers and 30 other workers in the business sim. That''s 90 characters with several attributes to each one. How would you write all that info into one class then write it to the file, and then load it back out? I understand how to open a file and write to it and then close it. Maybe i should state that this isn''t a 3D game, it''s text with some pictures on Windows platform. Thanx for all the replies, keep em coming!

Share this post


Link to post
Share on other sites
Hey,
The point is not to combine all of your data into one big class, but to combine all data for each individual character or whatever that needs to be saved. Only include the data you want to save in that structure though. Then, you can just save the whole structure to disk, or even an array of structures to disk, which is faster and possibly easier than writing all of the variables individually. Also, in my RPG, scripts can be activated on load time, so that they can make any more complex changes to the map state.

tj963

Share this post


Link to post
Share on other sites
First of all, if you need to write variable numbers of objects, you will probably want to create a sort of header for your save game file.

Basically, this header describes what is in the file. In this example, creating a header would probably involve first writing two integers to the file with the values 60 and 30, for the wrestlers and workers, respectively. Then, enter a for loop and write out information for all of these entities. For this example, you probably wouldn''t put all the variables in one class, but instead in a generic wrestler or worker class, for which there is one class object for each of the people you need to record.

Now, when loading the file back in, you need to read the header information in so you know exactly how many wrestlers and workers need to be created. Basically, you would do the reverse of the saving function; instead of saving data from existing objects, you would create new ones and load the saved data into them.

Hope this helps.



"Skepticism.... that great rot of the intellect." - V.H.
Bah, what does HE know?


Albekerky Software

Share this post


Link to post
Share on other sites