isSimSet(), isSimGroup(), etc
by Orion Elenzil · 08/14/2007 (9:42 am) · 5 comments
[preamble]
this is a very minor resource, but one i find handy.
in Torque, classes are derived from other classes.
eg, AIPlayer derives from Player, GuiControl derives from SimGroup, which derives from SimSet, etc.
so an AIPlayer is also a Player.
in script i sometimes would like to know if an object is of class player, or of any class derived from player.
thanks to typing, this isn't a problem in C++, but script is typeless so it's a bit of an issue.
i could do something like this:
if (%obj.getClassName() $= "Player" || %obj.getClassName() $= "AIPlayer"), but that's a pain in the butt and breaks as soon as i derive another class from Player.
[/preamble]
The following code is dirty, but it works, is efficient, and gets the job done.
I'd love to hear any C++ / torqueScript guru advice around how to generalize it further.
The approach is to create a virtual function along the lines of "IsClassFoo()" right down in SimObject, for which simObject always returns false, and then override the function to return true in the derived classes of interest.
SimBase.h
somewhere in the class definition for SimObject, for example right above "DECLARE_CONOBJECT(SimObject)
SimBase.cc
add this at the bottom:
aiPlayer.h
at the bottom, in a public section, add this:
do the same for player, simset, simgroup.
now in the script side, %obj.isSimSet() will return true for SimSets, SimGroups, GuiControls, what-have-you.
you can also call the isClassFoo() methods right from in C++,
as i believe they'll be somewhat faster than doing a dynamic cast.
eg, instead of this:
this is a very minor resource, but one i find handy.
in Torque, classes are derived from other classes.
eg, AIPlayer derives from Player, GuiControl derives from SimGroup, which derives from SimSet, etc.
so an AIPlayer is also a Player.
in script i sometimes would like to know if an object is of class player, or of any class derived from player.
thanks to typing, this isn't a problem in C++, but script is typeless so it's a bit of an issue.
i could do something like this:
if (%obj.getClassName() $= "Player" || %obj.getClassName() $= "AIPlayer"), but that's a pain in the butt and breaks as soon as i derive another class from Player.
[/preamble]
The following code is dirty, but it works, is efficient, and gets the job done.
I'd love to hear any C++ / torqueScript guru advice around how to generalize it further.
The approach is to create a virtual function along the lines of "IsClassFoo()" right down in SimObject, for which simObject always returns false, and then override the function to return true in the derived classes of interest.
SimBase.h
somewhere in the class definition for SimObject, for example right above "DECLARE_CONOBJECT(SimObject)
//-------------------- Debug Utilities ----------------------------
public:
virtual bool isClassAIPlayer() { return false; }
virtual bool isClassPlayer () { return false; }
virtual bool isClassSimGroup() { return false; }
virtual bool isClassSimSet () { return false; }SimBase.cc
add this at the bottom:
//-----------------------------------------------------------------------------
#define ConsoleMethodIsClass(function) ConsoleMethod( SimObject, function , bool, 2, 2, ""){return object->function();}
ConsoleMethodIsClass(isClassAIPlayer)
ConsoleMethodIsClass(isClassPlayer )
ConsoleMethodIsClass(isClassSimGroup)
ConsoleMethodIsClass(isClassSimSet )
//-----------------------------------------------------------------------------aiPlayer.h
at the bottom, in a public section, add this:
virtual bool isClassAIPlayer () { return true; }do the same for player, simset, simgroup.
now in the script side, %obj.isSimSet() will return true for SimSets, SimGroups, GuiControls, what-have-you.
you can also call the isClassFoo() methods right from in C++,
as i believe they'll be somewhat faster than doing a dynamic cast.
eg, instead of this:
SimObject* someObject; AIPlayer* bob = dynamic_cast<AIPlayer*>(someObject);you could do this:
SimObject* someObject; AIPlayer* bob = someObject->isAIPlayer() ? (AIPlayer)bob : NULL;
About the author
#2
but i didn't want to have to introduce new typemasks, seeing as there's only 32 of them available.
also i think if (%object.isClassAIPlayer()) is cleaner than if (%object.getType() & $TypeMasks::AIPlayerObjectType)
08/14/2007 (4:07 pm)
true enough,but i didn't want to have to introduce new typemasks, seeing as there's only 32 of them available.
also i think if (%object.isClassAIPlayer()) is cleaner than if (%object.getType() & $TypeMasks::AIPlayerObjectType)
#3
08/14/2007 (4:08 pm)
also getTypeMask() is only implemented in sceneObjects, not in simGroups etc.
#4
Did you have a look at this resource as an alternative?
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=11888
08/23/2007 (12:43 pm)
Hi Orion,Did you have a look at this resource as an alternative?
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=11888
#5
sorry for the delay in replying.
honestly, i can't quite wrap my head around that resource.
it seems pretty cool judging from all the comments,
but i haven't taken the time yet to understand what's going on.
so you may be right.
08/30/2007 (9:31 am)
hey William -sorry for the delay in replying.
honestly, i can't quite wrap my head around that resource.
it seems pretty cool judging from all the comments,
but i haven't taken the time yet to understand what's going on.
so you may be right.

Torque 3D Owner Daniel Eden
if (%object.getType() & $TypeMasks::PlayerObjectType) { // Do something for anything derived from player. }... and in code, it'd be:
if (pObject->getTypeMask() & ShapeBaseObjectType) { // Do something for anything derived from ShapeBase. }