Hi, my older question http://www.cplusplus.com/forum/general/238988/ was about creating an object factory. But now I have a problem I must save objects in vector<GameObject> to a file called Objects.dat. How can I do that?
Um... the same way as you'd write any other data to a file?
You'll need to decide what structure and format you want the file to have. But there's nothing fundamentally different about writing the value of an object's data member to a file, from writing the value of any other variable to a file.
You might want to look at overloading the streaming operators << and >> for your class. That can make life easier.
for (each element in vector)
{
myFile << first_data_member_of_element
myFile << second_data_member_of_element
// etc
}
Or, if you've overloaded the << operator:
1 2 3 4
for (each element in vector)
{
myFile << element
}
In other words, to access each element of the vector, you do exactly the same thing as you'd do to access the elements of any vector, and to write the values to the file, you do exactly the same thing as you'd do to write any other values to the file.
There's nothing magical about putting objects in vectors, that makes them work differently from vectors of, say, integers.
There's nothing magical about putting objects in vectors, that makes accessing the members of the object different from accessing the members of an object that isn't in a vector.
There's nothing magical about members of objects that makes writing them to file any different to writing any other values to file.
Personally I don't usually think much about the format but just make sure to read everything in the same order that I write. It has worked good for simple things that I have done but if your objects are complicated and share objects though pointers and such you might have to think more carefully how to make it work.
Here are some oversimplified pseudo code to demonstrate what I mean.
Don't take any of this too literally. You probably want to pass some kind of stream objects to the functions that you call the read and write functions on. You might prefer to have the save and load functions as member functions. You should also check for errors in the load functions. This is actually my favourite situation were I like to use exceptions.
But the important point that I want to make here is that each load function reads exactly as much data as the corresponding save function writes, and it does so in exactly the same order. When you write a save function you need to think about how the load function is going to read the data. If I had just saved each object, without saving how many objects there are, it wouldn't have been possible for the load function to know how many objects to load.
Must I save every variable on Gameobject manually?
Only the ones that need to be saved, that depends on your code.
The overloaded operator should look like this:
1 2 3 4 5
ostream& operator<<(ostream& oss, const GameObject& obj)
{
// you code here
return oss;
}
Is there a possibility that classes derived from GameObjects will be stored in the vector?
If yes then it might be a better idea to use virtual functions to save the object so it can be overriden.
The pseudocode I posted showed you two ways of doing it, one for writing each data member individually, and one for what you could do if you overload the streaming operators.