Archived

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

paul8262

Serialization...Problems i can see...

Recommended Posts

Up until now,iv''e been more interested in learning the cool stuff,such as shaders,effects,shadowing etc etc. So,now i''m getting into other problems such as memory management,resource management,the correct design patterns, and another problem(which i would like to discuss now)is SERIALIZATION. As many of you know,c++ doesn''t offer built in serialization(unless your using the MFC classes)which i''m not,so its a problem that i''m up against without much knowledge on. Streams....i am totally lost of how i would deal with pointers in this situation.A pointer(memory address)is never likely to be the same address,every time you run your game/application. There''s also a lot more things that are coming to mind(problems) i can see,with the way i have designed everything up until now,what about objects that are instanced?. If only i had come up against this problem in the beginning(or had some idea about it)which is probably not likely,as i didn''t know enough then,it might have saved me a lot of time. I can''t search the forum,to see if this topic has been discussed before in detail,as i am getting an error to contact the administrator etc. any help/answers/ideas...on this subject,any help at all on the best way to approach this is welcome and will be very much appreciated. Thanks, Paul.

Share this post


Link to post
Share on other sites
Well, you're right about pointers. They can't be serialized. However, you can save what the pointers point at. Say you have a pointer to a small structure you want to save. You can't save the pointer, but you can save the contents, like this:

thing_t *pThing = (thing_t *)malloc(sizeof(thing_t));

strcpy(pThing->Name, "Elite");
pThing->Gun = ENUM_RIFLE;
pThing->Life = 100;

ar << pThing->Name;
ar << pThing->Life;
ar << pThing->Gun;
When you want to load the details from a file, you simply re-allocate some memory for the structure, and load the details from the file:

thing_t *pThing = (thing_t *)malloc(sizeof(thing_t));

ar >> pThing->Name;
ar >> pThing->Life;
ar >> pThing->Gun;
Note that the pointer pThing has been re-initialized in the second codebite, and isn't the same memory location as the one in the first codebite. I'm using serialization for my level editor, and all my brushes, faces, shaders, entities, etc. are in pointer lists (I'm using MFC), so I've been optimizing this little method over the past week or two...

[Insert witty signature here]

[edited by - iNsAn1tY on October 14, 2003 4:08:07 AM]

Share this post


Link to post
Share on other sites
Thanks,i''m about to give this a try doing it this way.

Though someone else has told me to have a look through a couple of books(c++ for game programmers and Gem''s 3)as it has a(small) article about this in it using object factories and entity ID''s.

I have just borrowed a copy of c++ for game programmers,but don''t have a copy of gem''s 3 at hand.

I don''t know how good or how much the article (c++ 4 GP)goes into detail about it though.

Share this post


Link to post
Share on other sites