HasMethod - a way to tell if an object has a particular method
by Andrew Haydn Grant · in Torque Game Engine · 03/03/2005 (9:12 pm) · 2 replies
Does Torque have a built in way to tell if a particular object has a particular method? I poked about and couldn't find one, but by now I've learned that just because I don't find one doesn't mean it doesn't exist. :-)
Nonetheless, the following seems to do the trick, just slap it into simBase.cc.
Script usage:
So, two questions:
1) Does this functionality already exist?
2) If not, is there a better way to do it?
Nonetheless, the following seems to do the trick, just slap it into simBase.cc.
/// This returns true if the object has a method of the specified name, else false
ConsoleMethod(SimObject,hasMethod, bool, 3, 3, "obj.hasMethod(\"method name\")")
{
// Seems to be a listing of all methods that can be called on this object
Namespace *ns = object->getNamespace();
if (!ns)
return false;
// Convert the string to the common representation used in the string table
StringTableEntry method = StringTable->insert(argv[2]);
// Do we find the method name?
if (ns->lookup(method) )
return true;
else
return false;
}Script usage:
if ( %object.hasMethod("onSelect") )
%object.onSelect();
else
defaultOnSelectHandler(%object);So, two questions:
1) Does this functionality already exist?
2) If not, is there a better way to do it?
About the author
#2
However, I ran into some procedural code in the RTS selection script which did a case/switch on the "getClassName()" of an object. When I inherited from one of those classes in C++, my new class was not selectable! The "correct" solution would be to rewrite the selection code with OO method calls all the way up, but a hacky, faster, fewer bugs fix might use "hasMethod()".
I'm also tempted to write a thing which first tries to call a method, and then falls back to the datablock's method if that fails. This is actually very simple in script, but I fear it will make the script program flow even harder to follow.
For the most part, it is probably better to add the desired method way up in the namespace tree, and therefore *know* that it is present without having to ask.
03/04/2005 (1:02 pm)
I'm not likely to use it very often, actually. :-) However, I ran into some procedural code in the RTS selection script which did a case/switch on the "getClassName()" of an object. When I inherited from one of those classes in C++, my new class was not selectable! The "correct" solution would be to rewrite the selection code with OO method calls all the way up, but a hacky, faster, fewer bugs fix might use "hasMethod()".
I'm also tempted to write a thing which first tries to call a method, and then falls back to the datablock's method if that fails. This is actually very simple in script, but I fear it will make the script program flow even harder to follow.
For the most part, it is probably better to add the desired method way up in the namespace tree, and therefore *know* that it is present without having to ask.
Torque Owner Bryan Edds
I don't see any problems in the code, so if it works, use it :)
BTW, I'm just curious what you need this functionality for.