Help with polymorphism
by \"Robert Jaramillo\" · in Torque Game Builder · 02/18/2010 (10:56 am) · 2 replies
Hello, everyone
How do you go about deriving one class from another in torque script. I am not really sure how one would do this. If you could please give a small code example. My base class is fish.cs What I want to make is a class called crazyfish.cs derive from fish.cs. Any help or explanation is greatly appreciated, thanks.
How do you go about deriving one class from another in torque script. I am not really sure how one would do this. If you could please give a small code example. My base class is fish.cs What I want to make is a class called crazyfish.cs derive from fish.cs. Any help or explanation is greatly appreciated, thanks.
#2
Ah, and there is another pitfall here: many engine object classes do not have namespace linkage enabled. So, for this to work, make sure you are using an engine class that supports namespace linkage.
02/18/2010 (12:10 pm)
Ah, and there is another pitfall here: many engine object classes do not have namespace linkage enabled. So, for this to work, make sure you are using an engine class that supports namespace linkage.
Associate Rene Damm
Unfortunately, the mechanism in place here is pretty nonsense and allows for very little flexibility. However, for simple uses, it'll do its job.
There are two object properties that drive namespace linkage (i.e. method lookup hierarchies and thus the subclassing of implementation you want here) on objects: "class" and "superclass".
If "superclass" is present on an object, it will name the namespace that will be linked directly under the namespace of the class of the object being instantiated.
If "class" is present, it will name the namespace that will be linked directly under "superclass" (if available) or the class of the object being instantiated (if "superclass" is not available).
Finally, the object name will define its own namespace that is linked directly under "class" (if available) or "superclass" (if "superclass" but not "class" is available) or the class of the object being instantiated (if neither "class" nor "superclass" are available).
If you are confused by this, you are rightly so. This is both confusing and does not make much sense. You cannot have hierarchies more than 4 levels in total deep (native namespace down to object namespace) and there is no functional distinction between "class" and "superclass" other than introducing two levels instead of only one.
Using this is simple. For example, to put functionality common to all fishes in a shared namespace and then have specific types of fishes, you can simply do:
function Fish::biteIntoHook( %this ) { // ... } new ScriptObject( CrazyFish ) // or whatever native class { class = "Fish"; };To have another level in this hierarchy requires naming the superclass any time it is used:
function Fish::biteIntoHook( %this ) { // ... } function CrazyFish::biteIntoHook( %this ) { Parent::biteIntoHook( %this ); // ... } new ScriptObject( CrazyFishA ) { class = "CrazyFish"; superClass = "Fish"; }; new ScriptObject( CrazyFishB ) { class = "CrazyFish"; superClass = "Fish"; };