Original Post
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.
and a sample game struct:
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.
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.