Game Development Community

Script Inheritance

by Drethon · in Torque Game Engine · 07/15/2008 (6:18 am) · 8 replies

Can someone point me to the documentation on scripting inheritance? A brief search was unsuccessful.

#1
07/15/2008 (8:32 am)
Edit: www.garagegames.com/docs/tge/general/ch05s04.php

this is how you create a datablock and then inherit. SomeDatablock2 will inherit all the variables, etc from SomeDatablock.

datablock StaticShapeData(SomeDatablock)
            {
                myVar = 1;
            };

datablock StaticShapeData(SomeDatablock2: SomeDatablock)
            {
                
            };
#2
07/15/2008 (10:22 am)
@mb

Your example is backwards. It should be SomeDatablock2: SomeDatablock.
#3
07/15/2008 (10:26 am)
Oops. Yeh you're right.

edit: fixed my goof the code above should be correct now.
#4
07/15/2008 (11:01 am)
Having read through the datablock/inheritance guides I have a more specific question.

Can functions be inherited in torque script or am I right in thinking all functions are global so it doesn't matter?
#5
07/15/2008 (12:34 pm)
Yes. You add namespaces if you want to use functions. You create a datablock and add a classname variable. Then create functions for that datablock. Then you create another datablock and add the same classname variable and it will use those functions.

Namespaces - TDN

example:
The Ammo datablock (which isn't shown) has a className variable named Ammo. className = "Ammo";. The new datablock named CrossbowAmmo (shown below) also has this className variable. It will now be able to access the functions for the Ammo class. So it will be able to use the onPickup function below.

edit: actually I'm not sure if the Ammo datablock has the className variable, it just might use the datablock name for the className in CrossbowAmmo. Sorry if Im being too confusing.

datablock ItemData(CrossbowAmmo)
            {
                ...
                className = "Ammo";
                ...
            };

            function Ammo::onPickup(%AmmoDB, %AmmoOBJ, %Picker, %Amount) 
            {
               echo ("Calling Ammo::onPickup () ==> on ammo DB" SPC %AmmoDB);    
                %AmmoDB.DoIt();
            }

From tdn:
What this is doing is 'adding' a new namespace between CrossbowAmmo and ItemData, so that the namespace calling sequence will look like this: CrossbowAmmo -> Ammo -> ItemData -> et cetera.
#6
07/15/2008 (3:24 pm)
Hmm, I have a rough idea what you are getting at, just need to sit down and practice it to be sure I guess.
#7
07/16/2008 (12:11 pm)
Reading it again I think I get that CrossbowAmmo is of type ItemData but adding the className allows the calling sequence to check Ammo between ItemData and className. This allows for three level of "inheritance" (probably not the right word) as far function execution going but there is no data inherited from Ammo.

Based on this, I'm not sure a fourth level can be created in the script, might have to be done in the source...
#8
07/17/2008 (12:59 pm)
You could do:


datablock ItemData(CrossbowAmmo : Ammo)
{
...
className = "Ammo";
...
};

Anything from the Ammo datablock would be inherited.