Game Development Community

Class Inheritance

by Vern Jensen · in Torque Game Builder · 05/31/2007 (5:01 pm) · 4 replies

Just wondering how TorqueScript handles inheritance of classes and methods, or if this is even supported.

Let's say I have an enemyShip class. Then I have enemyShipRed, enemyShipGreen, etc. that extend the base class. So they share the same parent methods. How would you do that using script? (That is, create an object that extends a different object.)

And, how would overriding work in this case, if enemyShipRed has an explode() method, and enemyShip also has an explode() method... will the enemyShipRed's version be called if we create an enemyShipRed object and call .explode() on it?

-Vern

#1
05/31/2007 (5:21 pm)
Think of classes in torquescript more like namespaces/behavior assignment than actual inheritence. When you assign a class/superclass to a SimObject (or derived) it inserts it into the namespace list of the object (even the object's name is a top level namespace link).

function EnemyShip::explode( %this )
{
   echo( "boom Base" );
}


function RedShip::explode( %this )
{
     echo( "boom red" );

     Parent::explode();
}

%base = new ScriptObject()
{
    class = "EnemyShip";
}

new ScriptObject( RedInstance )
{
    superclass = "EnemyShip";
    class          = " RedShip";
}

%red.explode();

So for the above RedInstance would have the following namespace list:

RedInstance/RedShip/EnemyShip/ScriptObject/.../etc/etc

Think of it as declaring "class namespaces" then linking objects to those namespaces as needed

As an afterthought to mimic constructors/destructors you can use the onAdd/onRemove callbacks:
(and I usually create a static method to create instances too just to keep things clean if I only want certain types e.g. ScriptObjects or t2dSceneObject or whatever).

function SomeClass::CreateInstance()
{
    %r = new ScriptObject()
   {
       class = "SomeClass";
       // add any other member vars you need here
   };

   return %r;
}

function SomeClass::onAdd( %this )
{
   // do stuff - this is called when the instance is created and added to the sim
}

function SomeClass::onRemove( %this )
{
   // do stuff - this is called when the object is about to be removed from the sim and destroyed
}

%someInstance = SomeClass::createInstance(); // will call onAdd

... use it then later:

%someInstance.destroy();  // will call onRemove
#2
05/31/2007 (11:53 pm)
Wow, thanks for the detailed reply! I will go through that in detail soon, and probably try putting it into practice tomorrow. If I have any more questions, I'll post after I've had a chance to try it all out.
#3
06/01/2007 (11:32 am)
Just a quick correction to your source code, if anyone else is following this thread. When calling Parent::doMethod(), you actually need to pass it the instance of the object, such as Parent::doMethod(%this);

So far everything I've tried from your post has worked great, and is a tremendous help. Thanks again!
#4
06/02/2007 (2:17 am)
Vern is correct in the alternate method call syntax.

Normally when you call a method on an object, the Torquescript parser can immediately see the object that it's being called upon, and send the objectId along as the first parameter, but when you are calling a specific namespace method directly, you must provide the objectId to call it upon explicitly.

%myObject.explode();

vs

SomeClass::explode(%myObject);

Keep in mind that while it's provided if needed, this is an advanced concept, and shouldn't be used recklessly without knowing the underlying implementation of the core TGB classes themselves. For instance, it makes no sense (normally), given an object with a Class value of, say, Fish, but is an animated sprite, to ever call t2dSceneObject::onAdd(%myObject) for example, since internally the c++ code calls this up the hierarchy chain automatically.