Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Damocles

saving game data from classes

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

I was just wondering if it''d be possible (or advisable) to write a memory dump of a class instance to a file. Rather than having to save every variable manually, would it be possible just to dump the whole lot out in one go? And then to load it again, create another instance of that class type and read the data into memory as one big lump again. I seem to recall hearing something about virtual pointers causing problems in saving large smounts of class data, can someone elaborate where the trouble lies and any possible workarounds? I have a LOT of game data to save so manually saving every variable in every class will result in massive amounts of code just for this.

Share this post


Link to post
Share on other sites
Advertisement
If your class does not have "complex" attributes (char* szString won''t work while char szString[ 20 ] will work) all you have to do is open a file stream in binary mode and write the class directly to the file:
std::ofstream ofFile( "class.cls", ios::binary | ios::out );
ofFile.write( (char*)&SomeClass, sizeof( SomeClass) );
ofSettings.close();

and vice versa when you want to load it (using the read() method of ofFile...)



____________________ ____ ___ __ _
Enselic''s Corner - My site. Go test my game Spatra and see if you can beat it onto the Official Spatra Top 10. (source available)
CodeSampler.com - Great site with source for specific tasks in DirectX and OpenGL.

Share this post


Link to post
Share on other sites
Inadvisable. Virtual classes will royally screw up, pointers will of course be invalidated, and padding/byte ordering can become a problem as well, depending on how you''re using the files.

Just save every variable. It''s not that bad.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
Well, in Java, it's called serialization. I know that in Java if you do it and modify the class (for example, add an extra member), you won't be able to deserialize (read in) the classes you serialized (wrote in) before. In other words, just save every member separately, it'll save you some trouble.

[edited by - yaroslavd on June 17, 2003 6:39:01 PM]

Share this post


Link to post
Share on other sites
Can someone explain the exact problem with virtual pointers and class dump saves? Is it the case that the written data will actually write the vtable of every class as well as the other class dtaa, so when it loads back in, it gains the pointers from the prior program execution, which are no doubt completely wrong for the current execution? If so, is there no way around this?

What about simply dumping only the variables? Is it possible to do this? how is class data stored? perhaps its possible to calculate where the variable data begins and ends and only dump that?

Share this post


Link to post
Share on other sites
Just guessing here since I''m still a beginner:

If you want to be able to write a save routine for your class that doesn''t need rewriting even if you add new data members to your class, then I suppose you could always make a struct that contains all the variables you want and make the struct a member of the class. You could always get the correct size using sizeof() this way since the class''s vtable won''t be included in the struct. This doesn''t help with pointer data members of course.

Note: If you do reading/writing entire structures using sizeof() and reading/writing individual bytes, are you guaranteed to get everything back in the correct order? I have no idea.

Share this post


Link to post
Share on other sites
Yeah that''s an idea, don''t see why it wouldn''t work. But it would involve rewriting all the code so far in order to redirect to the struct for each class, which would be even more work than simply saving variables manually

As for pointer members, I don''t see any easy way around it really. For some I can simply store an array of pointers to objects, then reconstruct the array in the same order as saved, and simply save an array index. Single pointers arent so bad as long as you know what kind of class it is that has been saved. However, I do have a problem that a lot of my pointers are pointers to base classes. In these cases, it''s more tricky to determine what kind of class to recreate when loading the data

Share this post


Link to post
Share on other sites
From what I've read, the way around the pointer problem is to implement your own handle-based resource manager. Save handles, not pointers. I suppose this would be similar to your array idea.

[edited by - SpaceRogue on June 17, 2003 8:47:01 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by SpaceRogue
If you do reading/writing entire structures using sizeof() and reading/writing individual bytes, are you guaranteed to get everything back in the correct order?

Yes.




____________________ ____ ___ __ _
Enselic''s Corner - My site. Go test my game Spatra and see if you can beat it onto the Official Spatra Top 10. (source available)
CodeSampler.com - Great site with source for specific tasks in DirectX and OpenGL.

Share this post


Link to post
Share on other sites
quote:
Original post by yaroslavd
Well, in Java, it''s called serialization. I know that in Java if you do it and modify the class (for example, add an extra member), you won''t be able to deserialize (read in) the classes you serialized (wrote in) before. In other words, just save every member separately, it''ll save you some trouble.

[edited by - yaroslavd on June 17, 2003 6:39:01 PM]


Actually you can. Check out this and other serialization issues at:
http://www.acm.org/crossroads/xrds4-2/serial.html

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!