Game object connections

Started by
3 comments, last by rjackets 18 years, 8 months ago
Hey all, I'm after a little help if possible, with a system I'm trying to implement to maintain connections between objects. Basically the problem is trying to maintain a particle system for energy beams, where the beam connects 2 objects and either end of the beam must always match the position of the object it is connected to. This will be used for two purposes. Firstly to represent energy transfer between a variety of objects, and secondly to represent the attachment of the player to an interactive object in the level. So far, I've got a separate class to deal with energy transfer. Objects must inform the class when a connection is initiated. The energy transfer manager (ETM) then returns a unique identifier, which the objects both store in a vector of ints. Every frame, the object sends a message to the ETM containing its position and the unique identifier of any connections it has. After this, the ETM then traverses it's list of received messages and spawns a particle if the connection didn't previously exist, updates the particle if the connection still exists and deletes a particle if it fails to receive a message from both objects involved (if one message is received it can be assumed the connection is inactive as the other object must have been destroyed). If only one message is received, it sends a message back to the object telling it to remove the uID of the inactive connection from its list. In this way, we never have to check any pointers to objects, so if an object is destroyed while there is an active connection, we don't get any horrible crashes. Now this is all well and good, but I can't help but feel perhaps I'm making a mountain out of a molehill.... is there a simpler way to manage such connections without having to worry about objects that may have been destroyed? If anyone has any better suggestions I'd appreciate the advice!! Best Regards, Steve
Cheers,SteveLiquidigital Online
Advertisement
Global variables?

But seriously, sigslot: C++ Signal/Slot Library
It might be easyer to let the manager interogate the objects insteads of sending messages from objects every frame.
Ex: when you update your ETM and you check the status for every pair. You get the same result with fewer messages.
However you must remove the pairs related to one object when the object is destroyed.
maybe do some exception handling. im not real good with it in c++ (ive only dealt with Java exceptions before) but try to access that object (which may or may not exist) and if you catch an error, it means the object no longer exists or is invalid. this way, once you catch this error, maybe null pointer or something, you can discern that it doesnt exist and you can delete the pair connection from your ETM.
---------Coming soon! Microcosm II.
I agree with c0mas. The messaging system seems like it would take substantially more processing time than simply doing a check to see if the object still exists. I must admit, I wasn't sure about it in your post, i understand you have you energy transfer managment class, but do you also have a class/object for each beam? If not, I would recommend you make an object for each beam (the collection of which can be handled by your management class) -- this way, you can store the ID of the two objects the beam is connecting to and simply move the endpoints in relation to those two objects location (which should be stored in their own object). Each frame you should check to see if both those objects exist and if not, delete the beam object. Finally, if each object has a unique identifier then when the object is deleted that ID should also no longer exist. By checking to see if that ID exists you will not need to worry about refering to non-existant objects, which is much faster than messaging. Only problem with this is if a new object is spawned with the same id (since it is now available) the beam might attach to that object -- HOWEVER, this only applies to the frame RIGHT after the object is destroyed, since once it is detected to no longer exist the beam object should be terminated and deleted anyway.

(plus it is cleaner and easier to understand if each beam exists as it's own object)
I hope this isn't too confusing.

This topic is closed to new replies.

Advertisement