Skip to main content
GameDev.net gamedev.net
🔒 Locked

Please Critique my first I/O simple serialization attempt

Started by ViperG Jan 19, 2011 at 4:37 AM 1 replies 1.2k views
Original Post
ViperG
ViperG
So my game has lots of structures, all of which i will need to save at one point for save games and user created content with the in game ship builder. After doing some thinking on how to do this in the easiest possible way with the least amount of maintenance and code, i came up with this method, please give me your opinions, this is a prototype wip and it's rather simple and currently isn't finished but i wanted feedback on it.



template<typename t3>
void ioprocess(t3& data, string iotext)
{
if (method==SAVE)
{
if (iotext.size() > 0)
FOI << iotext << data << endl;
else
FOI << data << endl;
}
else
{
string itext="";
FIO >> itext;
if (itext==iotext)
FIO >> data;
else ; //throw a error message...
}
}



and a sample game struct:



struct shipdata
{
bool bumpmap;
string descrip;
int tid, schid, lid, shid, mass, armor, hull, vents, ventcolor, dtid, dsmall, dbig, bumptid, collision, explosion, ai, price;
float xsize, ysize, armortype, thrust, shscale, shieldangle, trscale, txsize, tysize, scale;
string owner, sclass, sid, name;
vector<datacomp> Components;

void shipdata::processio()
{
ioprocess(sid, "$sid= ");
ioprocess(name, "name= ");
ioprocess(descrip, "description= ")
...
...
...
}
}



Right now FIO and FOI are going to be global input/output ifstream/ofstream. or i can pass it as a referenced parameter but in this situation 2 globals are easier.

so for example if I have 100 instances of ship data, i can just call shipdata.processio(); for each instance, and it will save the entire structure into whatever file I have specified, and the same command can be used to read just by toggling which method i want to use, save or load.

and if I add members or remove members of the struct, I simply add a ioprocess call for that variable and saving and loading are done for me.
and this method can be put on all my structures or classes if I want to save any of their members or load them.
Black Sky A Star Control 2/Elite like game
ViperG
ViperG
75 views no comments ... bump.

Ok let me ask the question a different way.

If you were going to serialize a class or structure someone like the one i showed, would you do it in a way like i did or would you do it completely different (aka a BETTER way?)
Is there a more efficient or robust method of doing what I am trying to do...? Does my method have an apparent flaw?
Black Sky A Star Control 2/Elite like game
Drew_Benton
Drew_Benton
There is not much to critique really. There is certainly nothing wrong with the direction you are going, but you might want to consider passing your objects by reference rather than value for the object. That way, you don't invoke any unnecessary copies for more heavy objects that override the stream operators you are using. Also the logic you are using of writing out and then reading back in is not consistent with the inputs you allow. For example, if you were to write out a field with no name, your reading code would break. Consider just writing a newline in place of the name to keep the format balanced no matter what. Then you can check to see if it's an empty line and if so, read in the data and ignore the iotext name check. Other than that, that's just about the way you handle the simplest versions of serialization in text format.

I'd really suggest that you take a look at an existing serialization solution though, such as boost::serialization. It might seem overwhelming at first, but here is another set of tutorials that should be a lot easier to follow and understand to get you started. From the perspective of using something in the short and long term, I think you'd be making a better investment with boost rather than your own. That way, you can say you've used an existing solution and you either will use it in the future because of X, Y, and Z or you will say you won't use it again because of A, B, and C and then implement the differences into your own code.

That's about it really. You might want to check out the following article as well: [36] Serialization and Unserialization for more information on the topic.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.