Game Development Community

Inheriting object methods in Torque Script

by Fenrir Wolf · in Torque Game Engine · 06/16/2004 (12:47 pm) · 10 replies

Okay, I'm a bit confused here. Let me try to give you a bit of background so you can see where I am coming from.

I want to create a generic object type that is based upon a StaticShapeBase datablock.

datablock StaticShapeData(MyBaseObjectData)
{
  shapefile = "blah" ;
  blah = foo ;
  etc. ;
}

I also want to attach methods to this object.

MyBaseObjectData:onAdd (%this, %obj) {
  do_stuff() ;
}

Alright, that works fine. But now I want to create specific items and then override datablock settings. For example:

datablock StaticShapeData(MyUniqueObject : MyBaseObjectData) {
  shapefile = "something else" ;
}

It works fine, insofar that when I drop the object in the world, it loads the second shape file as I expect it to. But it seems like methods don't inherit. For example, the onAdd() doesn't get called at all.

So, how would I be able to create a generic datablock class and then premutate it for any subclass objects without having to cut and paste all of the code found in OnAdd() and such for each one?

Looking at Items, it seems there is a generic class called "Item" and then a datablock type called "ItemData." But that's handled in engine code. Would I have to create a new datablock type, called "MyBaseObjectData" in the engine?

I'm a bit confused on how inheritance works in scripts.

#1
06/17/2004 (6:00 pm)
Shouldn't your datablock have a classname property

such as :

datablock StaticShapeData(MyUniqueObject : MyBaseObjectData)
{
classname = "MyBaseObjectData";
shapefile = "something else" ;
}

Otherwise, AFAIK, both myUniqueObject and myBaseObjectData would inherit their methods from StaticShapeData not from each other.
#2
06/17/2004 (6:45 pm)
Bruno is correct, you should just need to set the className field to "MyBaseObjectData" and all should be well. :)

For example, create a new simple test with a new main.cs file containing the following code:

setLogMode(2);

//global functions
function do_stuff() {
    echo("do_stuff() is... doing stuff");
}

//MyBaseObjectData DataBlock definition
datablock ShapeBaseData(MyBaseObjectData) {  
    blah = "blah" ;  
};

function MyBaseObjectData::myTestFunction () {  
    do_stuff();
}

//MyUniqueBaseObjectData Datablock definition
datablock ShapeBaseData(MyUniqueBaseObjectData : MyBaseObjectData) {     
    className = "MyBaseObjectData";
    iAmUnique = "I am unique!";
};

echo("start method inheritence test");

echo(MyBaseObjectData.blah);
MyBaseObjectData.myTestFunction();

echo(MyUniqueBaseObjectData.blah);
echo(MyUniqueBaseObjectData.iAmUnique);
MyUniqueBaseObjectData.myTestFunction();

echo("end method inheritence test");

quit();

function onExit()
{
}

Examining the console.log file will show that the method inheritence worked. (Sorry about the vertically-compressed code in the above, I still don't know how to make the forums code tag show code exactly like you type, whitespace included.)

Good question! We will be updating the scripting docs soon, to make stuff like this more clear.
#3
06/17/2004 (8:54 pm)
Whew! Glad it was something simple. Thank you, Bruno and Josh. It's pretty clear now. Datablock inheritance is more like just a value copy, whereas method inheritance happens on an entirely different level.

Works like a charm, all of my sub-class objects are behaving as I expect them to. :)
#4
11/30/2005 (2:28 pm)
I have the following datablock inheritance but onReachDestination doesn't get called for DemoPlayer2, any idea why? Thanks

datablock PlayerData(DemoPlayer : PlayerBody)
{
shootingDelay = 100;
shapeFile = "~/data/shapes/player/player.dts";
};

datablock PlayerData(DemoPlayer2 : DemoPlayer)
{
shootingDelay = 200;
shapeFile = "~/data/shapes/player/playerOLD.dts";
};

function DemoPlayer::onReachDestination(%this,%obj)
{
echo("reached");
}
#5
11/30/2005 (2:43 pm)
Silly goose, the answer is already in this thread!

Change you DemoPlayer2 block to this:

datablock PlayerData(DemoPlayer2 : DemoPlayer)
{
className = "DemoPlayer"; // ADD THIS
shootingDelay = 200;
shapeFile = "~/data/shapes/player/playerOLD.dts";
};

knowhere.net/
#6
12/01/2005 (1:08 am)
@Brian thanks for the reply but onReachDestination wasn't called by the callback. If i call it directly (player2.onReachDestination()) it works but not through the callback throwCallback("onReachDestination"); in AIPlayer.cc.
#7
12/01/2005 (10:06 am)
Can you post your DemoPlayer2 datablock definition?
#8
12/02/2005 (12:42 am)
@Brian isn't that the definition, becuase I don't have anything else. Since DemoPlayer (which only has the definition I posted above) inherets from AIPlayer I assumed that if I make DemoPlayer2 inherets from DemoPlayer it will also inhert the parent (AIPlayer).

Thanks
#9
07/15/2009 (8:07 pm)
@Ahmed - you ever get this answered? I'm having same issue and question as you had in this post.

Thanks in advance...

Peter
#10
09/09/2009 (9:33 pm)
Peter,
you can find more info about inheritence at this post