Save Game

Started by
6 comments, last by Saint Squireen 11 years, 9 months ago
(Straight to the point) I want to save health, level, experience, coins whenever needed and then close the application and then restart the application and recall health, level, experience, coins whenever needed.

Is there any real good ways of doing this? A google search has turned up nothing useful and I could use some help.

~Saint Squireen
Advertisement
The technique you're looking for is called serialization. Serialization is the process of writing out data from your application to a file (or any other external medium) so that it can be loaded back in at a later time.
Depending on the language you're using you could already have the tools to do this quite easily. A lot of managed languages already provide tools for easily writing out the contents of your classes or structures to binary files, text files, xml files, etc.

I gets all your texture budgets!

i am currently using C++.

~Saint Squireen

i am currently using C++.


C++ does not provide any built-in tools for serializing classes, so you'll have to provide these yourself.
A common way of doing this is to create a Serializable class which you use as a base for all your classes you want to be able to serialize. This class could contain a pure virtual function called serialize which takes a filename as input. You then implement this function for every child class, and you write out the all the data for your class to the file with the given filename individually using the the normal C++ file IO mechanisms.
If you want to export your classes as text data you'll have to make sure you first of all create valid string representations of your class members.
The only thing you have to do then is reverse the process described above so you can construct or populate classes from file data. You could use a factory pattern to achieve this this for example.

If you want to elaborate on this technique some more you could build an attribute system where you first convert your class data to a set of attributes which you can then export any way you want with a generic attribute serializer. This would also allow you to collect all the data you'd want to save before doing any actual exporting.


Another way I've seen serialization done is to overload the stream operators for your class so you can export your class data to a stream. I haven't made any extensive use of this approach though, so I couldn't tell you any up- or downsides of this technique.

I gets all your texture budgets!

ok thanks for the insight :)

~Saint Squireen
Another thing I forgot to add:

If you just have to write out a small amount of values then a full serialization system might be overkill, you can always just use the file IO systems provided by C++ to write your values out to a binary or text file. An actual serialization system should come into play the moment where you find yourself needing to export/import entire class states.

I gets all your texture budgets!

See:
http://en.wikipedia.org/wiki/Serialization#C_and_C.2B.2B
http://stackoverflow.com/questions/234724/how-to-serialize-in-c
http://www.parashift.com/c++-faq-lite/serialization.html
http://www.ocoudert.com/blog/2011/07/09/a-practical-guide-to-c-serialization/
http://gamedev.stackexchange.com/questions/2269/what-are-good-solutions-for-serialization-in-c
thanks guys!! :)

~Saint Squireen

This topic is closed to new replies.

Advertisement