Game Development Community

Simple Code Question - Checking strings for NULL

by Nicolai Dutka · in Torque Game Engine Advanced · 05/26/2009 (8:15 am) · 3 replies

I was never taught this one, but it seems simple and vital...

I have a function that CAN be passed the name of an object, but doesn't require it:

function makeTestObject(%mesh,%tex,%end,%child)
//%mesh = which mesh do we use? integer value 1-10
//%tex = which texture to give our mesh? integer value 1-5
//%end = Z position where we want our object to stop
//%child = which object do we activate once we are done with this one? string value = name of object

So then later, I have code that can move that test object to a specific Z position. When the object reaches that position, I want to start moving the child, but only if one exists. I know I can do simple things like:

activate(TestObject.child);

Assuming that 'activate' is a valid function and my "makeTestObject" used %child as such:

TestObject.child = %child;

Which it does...

But, what if %child was left blank? Is this ok? I am thinking the worst that will happen is that I will get a console error: Cannot find object ''. Attempting to call function 'activate'. (Something like that).

What would be nicer though would be something like:

if(TestObject.child != NULL) activate(TestObject.child);


Only thing is, that line doesn't work. So, how do I check for a NULL value in a string variable?

#1
05/26/2009 (9:19 am)
Try
if( isObject(TestObject.child) ) ...

It will tell you if an 'object reference' is an existing object.

Nicolas Buquet
www.buquet-net.com/cv/
#2
05/26/2009 (9:25 am)
TorqueScript strings are never null but rather empty instead. So the proper way to check for this is

if( TestObject.child !$= "" )
   activate( TestObject.child );

//EDIT: as for testing whether TestObject.child refers to a valid object, Nicolas is right, of course.
#3
05/26/2009 (9:37 am)
Beautiful!!! Thank you both VERY much!! Both of those work for me, and I also developed a 3rd option while I was waiting. When my world builder make the object, they stick a "0" in for %child if there isn't one, then I simply:

if(! (TestObject.child $= "0") )  activate( TestObject.child );

It's really cool that we can have multiple solutions to the same problem. :P