Jump to content
  • Advertisement
Sign in to follow this  
tentoid

Serializing and pointers

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

Greetings! I have been writing serialization functionality in to my current game project. Currently the system works quite well but I have one small problem I'm not sure how to solve. I have a class Actor (NPC) and Actors have a pointer to an AI object. When a NPC is attacked the AI object is told who attacked the actor and the AI object gets a pointer to the enemy actor. My classes look something like this:
class Actor { // aka. NPC
  AI *myAI;
};

class AI {
  Actor *parent;
  Actor *enemy;
};


Currently my classes inherit from SerializableObject class. Each object serializes itself and the objects it owns like so: Maps serialize themselves and call the serialization method of the NPCs and items they own. NPCs then call the serialization method of the AI object which controls them. So when serializing a NPC object, the NPC calls it's AI object's serialization method. Now the AI should remember who his enemy was but the AI object can't serialize the enemy object because the NPCs might already be serialized (the Map object serializes the NPCs it owns). Now how could I make the AI remember it's enemy when serializing? I hope someone understood what I'm trying to do here.

Share this post


Link to post
Share on other sites
Advertisement
Rather than load/store a pointer, you load/store a unique ID that represents the object. You probably also need to include the ID with the object itself (unless the ID is simply an index).

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnBolton
Rather than load/store a pointer, you load/store a unique ID that represents the object. You probably also need to include the ID with the object itself (unless the ID is simply an index).


Ok, thanks. I was thinking that something like that might be the answer. Now I just have to think a good way to create unique IDs.. Probably a simple static int variable will do.

Share this post


Link to post
Share on other sites
I just use the memory address of the object as the unique ID, this works fine since I don't copy my objects, they just reside one place in memory.

[Edit] Seems like John was faster than me
\Thomas Mølhave

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!