Game Development Community

A clear explanation of isObject()

by Takara Mitsumi · in Torque Game Builder · 04/06/2010 (9:42 pm) · 6 replies

Can someone please clearly explain how to properly use isObject? The documentation doesn't explain this at all. It only uses it in code.
I am trying to compare two tiles on a 2d grid and I saw the isObject syntax and thought this means:

if(isObject(objectName) => "if this object is X, then do this"
...
do this
...


but I'm not sure if this is what it means. If it does mean this, then where do I get an object name? for example, if I'm trying to compare two objects, in the "Scripting" dropdown box in TGB, there's a space for "Name." so if I use isObject, do I use the name I put in the Name box?

If I put "Red_piece" in the "Name" box, then when comparing 2 objects, do I write:

if(isObject(Red_Piece) //if this object is called "Red_Piece" then do this
...
do this
...


Is this right? I hope I am clear enough on what I'm trying to ask. Thanks in advance.

#1
04/06/2010 (11:35 pm)
Look here and scroll down to "Handles and Names". That explains how names and object-ids are equivalent in TorqueScript.

The "isObject( XXX )" does just that and checks to see if XXX represents an object. If this is an object-id (integer) or a named object (alpha-numeric) then it'll return true else false.

So yes, if you name an object (which must be unique) then you can access it via its name so if that was "Takara" then "isObject(Takara)" would return true. If you did "isObject(Mitsumi)" but no object existed by that name then it'd return false.

Also, doing "Takara.getid()" returns the objects ID. If that was say "1234" then doing "isObject( 1234 )" would also return true as would "isObject( Takara.getid() )";
#2
04/07/2010 (5:53 am)
isObject() verifies that the object exists:
if(isObject(myObject))
   //do something here...
this code checks to see if 'myObject' exists. If it does exists, the code contained in the if statement is executed because isObject() is a function that returns a boolean value(true or false)
%myObjectExists = isObject(myObject);
You don't want to pass a string to this function such as textbox input. Instead, pass an object id (myObject.getId()) or the name of the object.
If you want to check the text in a textbox:
if(tbName.text $= "Whatever")// i think its .text. may wanna verify that
   //do something here...
else
   //do something else here...
#3
04/07/2010 (6:00 am)
Thank you very much!
#4
04/07/2010 (9:00 am)
One last question: is there a way to convert the ID # of an object to the object name?

For example, if the ID# of an object is 123, is it valid syntax to write:

%myObject = redPiece.getID();
echo(%myObject.toName());
#5
04/07/2010 (9:20 am)
You use "getName()" on any object to retrieve its name (assuming it has one).

If your object was called "fred" and its id was "123" then:
echo( fred.getID() ); // outputs "123".
echo( fred.getName() ); // outputs "fred"

echo( 123.getID() ); // outputs "123"
echo( 123.getName() ); // outputs "fred"
#6
04/07/2010 (9:26 am)
Thank you again!!

My problem was that I don't know what the ID of an object is initially, so that was why I was wondering if that was possible.