views:

648

answers:

6

I went in late in a project of gamedev of a simple text game in C++.I studied the project and I think I know how to do everything but i get stuck on a thing:how to save and load the game as proposed.I don't want to have the thing done for me,only you show me a way to do it.

+6  A: 

Well the simplest way I can think of is to have a structure/class which represents the whole games "state." And on save you simply write this entire structure from memory to disk. Loading would be the opposite.

What format it is on disk is entirely up to you, if your state consists entirely of simple data types, you may just want to write a binary blob. Or you might want to go with something more versatile such as serializing it to XML, it's really up to you.

Evan Teran
+3  A: 

The process you are describing is known as "serialization"- that is, taking an in-memory object and converting it into some form that can be saved to disk. It could be as simple as a raw memory dump, or something complicated like XML.

I suggest taking a look at the wikipedia article on this subject: http://en.wikipedia.org/wiki/Serialization

igowen
A: 

It all depends on how complex your save files would be. If it's just a few integers than it's probably pretty easy to simply write a couple of functions that would write those values to a file in a specified order and then load them in the same order that they're written. If it needs to be more complex and handle saving the values of multiple objects than serialization is your best bet. If you're looking for a human readable save format I would suggest XML Serialization, otherwise you may want to look up some non-human readable formats.

+1  A: 

XML serialization seems the easiest solution. With small lightweight libraries such as TinyXML it's even easier. As for making the files unreadable you can use simple XOR obfuscation with a short key. Or you could try JSON.

frgtn
+1  A: 

I didn't read the website you linked to, but you'll probably want to write code that reads/writes to a basic text file in order to load/save your game.

First, figure out how to format your text file. For example, if I'm writing a pacman game that allows the player to save games, the text file might look something like this:

5 
54700
3

I would write my code to read the file one line at a time:

Line 1: the level the player is on.

Line 2: the number of points the player has.

Line 3: the number of lives the player has.

Do a Google search for C++ file input/output to figure out how to write the actual code.

Michael Angstadt
+3  A: 

I've only taken a quick look at the post but here's my suggestion:

You game state might consist of a couple of structures and a few arrays of data. The structures would define the player(name, health, money, whatever), a weapon (attack, chance, etc), armor etc and so on...

First you would write the player's state by saving his structure. Next would would be the number of weapons owned by the player, followed by an array of weapon structures, the same for armor...

The file format would then be

STRUCT player
INT num_of_weapons
STRUCT weapon
STRUCT weapon
STRUCT weapon
INT num_of_armor
STRUCT armor
STRUCT armor

It can be easily done using fstream.

HyperCas