Jump to content
  • Advertisement
Sign in to follow this  
kbrizs

Need assistance with understanding a game save system

This topic is 3265 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

Can someone give me a good example on how to create a save game system. The idea would be a tile based game that has the player character and a couple of npc's. This is in C++. To be more specific, what would be the best way to hold the characters in memory and the best way to save them out to a file as well as restore them. I would like to keep it simple. This would be an SDL game, but no SDL code is required if you can help. Just a good explanation on how the objects should be represented in C++. Looking for code examples. Here is a little more description. For example A class called NPC, with member variables x and y for the position on screen. Keeping it extremely simple. Than there is a PC class which would be exactly the same. Having two separate classes, what would be the best way to save the required information to file and restore it at a later time. Also, should I be considering anything else ahead of time. Should there be other content to be saved and etc. Any help on this would be appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Heya!

Have you worked with the C++ file streams before?

std::ifstream
std::ofstream

It's really just another ambiguous system to work with where you decide everything that goes in and out of a file. What I'm trying to say is, This is a question that has no "right" answer. [smile]

If you were trying to save simple things like position values on a tile based game, I imagine you'd only really need 3 values at minimum.


  • The entity's id. (Player, Enemy1, Enemy2, ...)

  • X Position

  • Y Position

Once you know what you need, it just becomes a matter of knowing when and where to read and write data.

If you had an object called cEntity, you would just use the file streams to read and write it's data and go about your day. Here's a quick & dirty example that simply demonstrates what you would do if you had only one save file to use.


class cEntity
{
long id,x,y;

public:
bool read( std::ifstream& f )
{
// Assert f.good() == true
f.read((char*)&id, sizeof(long));
f.read((char*)&x, sizeof(long));
f.read((char*)&x, sizeof(long));
}

bool write( std::ofstream& f )
{
// Assert f.good() == true
f.write((char*)&id, sizeof(long));
f.write((char*)&x, sizeof(long));
f.write((char*)&x, sizeof(long));
}

};



Quote:
Also, should I be considering anything else ahead of time.


When it comes to stuff like this, that's really a question you should be answering. We have no creative control over your project, and have no idea what your plans are for the game. It's helps to plan this stuff out beforehand! [smile]

I hope this helps.

Cheers!
-Zyro

Share this post


Link to post
Share on other sites
The example above is actually exactly what kind of answer I was looking for. As for the game, the idea is to make a small skeleton program which I can expand later, so there is not much more to consider. The tile save is a good start, which for example brings up some more basic questions. BTW, yes I have worked with streams before, so even though I am looking for very basic examples, feel free to throw in several C++ concepts, but if possible please stay out of the STL for now.

OK, the basic question is how you have the class setup. I am a little closed minded in OOP. Would I want to create a file handling class that the entity object inherits from to keep it modularized? Or is there another method for this. What would be most efficient? That is very important, I mean to be small in memory and etc.

Share this post


Link to post
Share on other sites
Quote:
Original post by kbrizs
OK, the basic question is how you have the class setup. I am a little closed minded in OOP. Would I want to create a file handling class that the entity object inherits from to keep it modularized? Or is there another method for this. What would be most efficient? That is very important, I mean to be small in memory and etc.


Perhaps these game objects could inherit from a "Serializable" class, overriding its virtual methods "read" and "write", which both take file stream pointers as arguments. This is basically an interface class and need not provide implementations

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!