Game Development Community

Check for object existence

by Rob Segal · in Torque Game Builder · 08/06/2006 (6:39 am) · 9 replies

If I want to check for the existance of an object in the scene graph is there a preferred way to do this?

#1
08/06/2006 (8:49 am)
Boolean isObject();

If it returns true, object exists, if false, object doesn't exist.

if (isObject(myCoolObject))
{
    // do something
}
#2
08/06/2006 (8:57 am)
What do you know about the object? Do you know the ID? Or the class and that there can only be one of that class?

If you know the ID and want to know if it is in a certain scenegraph you can do this:
function isObjectInScene(%object, %scene)
{
   return %object.sceneGraph == %scene;
}

You can also iterate your scenegraphs like a regular SimSet:
for( %i=0; %i < %sceneGraph.getCount(); %i++ )
{
    %obj = %sceneGraph.getObject(%i);

    // do something with the object
}

// or use

if( %sceneGraph.isMember(%obj) )
 ....
#3
08/06/2006 (9:37 am)
That is some hot sh--! Thanks Martin and Mike. Martins response is exactly what I was looking for Mike but I definitely found your reply handy as well. Didn't know scene graph objects were maintained in a SimSet.
#4
08/06/2006 (11:47 am)
As far as I remember the function isObject does check if the object's ID is 0 (zero). So you could also check for a 0 if the object exists like

if (ThisIsAnObject  == 0)
{
    // do something
}

But best solution I guess would always be to use the isObject() function.
#5
08/06/2006 (6:22 pm)
Checking for zero won't reliably work because you could just feed it a bogus id, say "3714", and it will tell you that is an object. However, the object referred to by 3714 may have been deleted. The "isObject" function verifies the existence of an object, not just making sure it isn't zero.
#6
08/06/2006 (7:32 pm)
You're right, Ben. Usually, when not feed with a bogus id, checking for 0 works too, but, as you said, not 100% reliable. So isObject is always the best choice :-)
#7
08/07/2006 (2:34 pm)
Please check the source before post
From simBase.cc 533:
ConsoleFunction(isObject, bool, 2, 2, "isObject(object)")
{
   argc;
   if (!dStrcmp(argv[1], "0") || !dStrcmp(argv[1], "")) // <-- LOOK THIS
#8
08/07/2006 (2:56 pm)
Whom do you mean Adam?
#9
08/07/2006 (4:27 pm)
The full function looks like this:

ConsoleFunction(isObject, bool, 2, 2, "isObject(object)")
{
   argc;
   if (!dStrcmp(argv[1], "0") || !dStrcmp(argv[1], ""))
      return false;
   else
      return (Sim::findObject(argv[1]) != NULL); // <-- THIS IS THE IMPORTANT PART
}