Safe Pointer's to SceneObjects
by William Todd Scott · in Torque Game Engine · 07/22/2006 (8:13 am) · 4 replies
Hey all,
I am writing a component that will be used by turrets, mounted weapones, etc. to auto target objects that are withing a search area. When a target is found, the component maintains a pointer to the SceneObject.
My question has to do with the safest way to ensure that the object that has been locked on to is still valid (e.g. wasn't destroyed by us or someone else).
I have three thoughts on how to do this, but wanted to see what other people thought...
TOO SLOW(?):
1) Instead of maintaining a pointer to the object, maintain its SimObject id and look up the object everytime I need to access it. If it is not found, I know it was deleted.
SEEMS GOOD:
2) I have a publisher/subscriber based messaging system in place. Use that to listen for objects being deleted and verify that the targeted object hasn't been destroyed.
NEED SOME GUIDANCE:
3) I believe that there are 'smart pointers' build in to Torque, but I haven't used them. Is this something I should look into? Can someone point me to an example of where this type of functionality is used?
Thanks
Todd
I am writing a component that will be used by turrets, mounted weapones, etc. to auto target objects that are withing a search area. When a target is found, the component maintains a pointer to the SceneObject.
My question has to do with the safest way to ensure that the object that has been locked on to is still valid (e.g. wasn't destroyed by us or someone else).
I have three thoughts on how to do this, but wanted to see what other people thought...
TOO SLOW(?):
1) Instead of maintaining a pointer to the object, maintain its SimObject id and look up the object everytime I need to access it. If it is not found, I know it was deleted.
SEEMS GOOD:
2) I have a publisher/subscriber based messaging system in place. Use that to listen for objects being deleted and verify that the targeted object hasn't been destroyed.
NEED SOME GUIDANCE:
3) I believe that there are 'smart pointers' build in to Torque, but I haven't used them. Is this something I should look into? Can someone point me to an example of where this type of functionality is used?
Thanks
Todd
#2
I write about this in my second book (not yet published). Here is an excerpt:
Hall Of Worlds - For Gamers
EdM|GPGT
07/22/2006 (8:49 am)
@William - Hi. Yes, Torque has a smart pointer class. Torque's smart pointer class will automatically get set to NULL when an object it is pointing to is deleted. Thus, checking for a valid object is as simple as checking for NULL.I write about this in my second book (not yet published). Here is an excerpt:
////
// Create a smart pointer to contain a GameBase reference
//
SimObjectPtr<GameBase> mOrbitObject = NULL;
////
// Create a second standard pointer for demonstration purposes
//
GameBase * pOtherPointer = NULL;
////
// Locate an object by the name "theCamera" and store a reference to it in
// the smart pointer and the (dumb) standard pointer:
//
mOrbitObject = pOtherPointer = Sim::findObject("theCamera");
////
// Loop while the SMART pointer is not NULL
//
while( NULL != mOrbitObject ) {
Con::printf("The ID of theCamera is %d" , getId() );
////
// Tell object to unregister and then delete itself, using the dumb
// pointer.
pOtherPointer->deleteObject(); // we will talk about this method later...
}Quote:
The above code will find an object by the name "theCamera" and store a reference to it in both a standard (dumb) pointer and a smart pointer. The code then loops until the smart pointer is NULL. Inside the loop, we print the objects ID and then tell it to unregister and to delete itself. The key thing to notice is that this unregister/delete request is made with the dumb pointer, but that the smart pointer is automatically updated to NULL. Of course, the dumb pointer will still have an address in it (now invalid), which is why it is dumb.
Hall Of Worlds - For GamersEdM|GPGT
#3
I'm going to dig into and start using the built in smart pointers.
Todd
edit: Because of the examples you guys provided this took me about two seconds to implement. Thanks again!
07/22/2006 (9:24 am)
Thanks guys, I really appreciate the guidance.I'm going to dig into and start using the built in smart pointers.
Todd
edit: Because of the examples you guys provided this took me about two seconds to implement. Thanks again!
#4
07/22/2006 (9:46 am)
Sweet!
Associate Orion Elenzil
Real Life Plus
2. this is actually the one i would choose last. it just has more moving parts than 1 or 3.
3. SimObjectPtr. Defined in simBase.h. An apropos example is mAimObject in aiPlayer.h. I think i would choose this method.