Game Development Community

noob inheritance problem!

by jerryg7rk3w · in Torque Game Builder · 11/02/2011 (11:19 am) · 20 replies

datablock doit(Ammo)
{

class = "Ammo";
//more stuff to inherit
};


datablock ItemData(CrossbowAmmo : Ammo)
{

class = "Ammo";
//stuff to inherit
};

function Ammo::onPickup(%this)
{
echo (" testing 1 2....? " SPC %this);
%this.itemdata(); // %this.crossbowammo(); %this.ammo(); <===none of these work either
}
I'm doing something wrong with this inheritance code but don't understand what.
I have a t2danimatedsprite object in the scene named test with the class of ammo, so i'm calling it from the console with test.onpickup();.

#1
11/02/2011 (4:12 pm)
Moved to the correct category for more exposure.
#2
11/03/2011 (7:04 am)
Really no one has an answer to this easy noob question, come on mr garagegames employee (christopher tauscher) id think you would know this one by heart.
#3
11/03/2011 (7:32 am)
What exactly are you trying to do with the lines of code which "don't work"? Reading the code makes it look like you are trying to call 3 different functions. Do you actually have a function called crossbowammo? Or one called ammo?
#4
11/03/2011 (8:25 am)
Most likely nobody's answered because you're question is a kind of a mess. I'm not sure what you're trying to do, so I'm not sure what the right answer is.

Sorry.
#5
11/03/2011 (9:00 am)
oh i'm sorry man my bad, i'm just confused about an inheritance issue that i'm having.
I don't understand how the inheritance works with torque game builder at all, all i want to do is understand how to inherit code from a datablock with a function.
I have tried to learn from this example before at: http://tdn.garagegames.com/wiki/TorqueScript/Namespaces

function GameBase::doIt(%this)
{
echo ("Calling StaticShape::doIt() ==> on object" SPC %this);
};

but if you plug the function gamebase::doit(%this) into torsion i get an error right off the bat because the example wasn't meant to be working code, that's confusing someone like me who is new to the concept.
Id appreciate the help if you could explain how to inherit code from a datablock using the class and superclass with a function, with some working code thanks.
#6
11/03/2011 (9:39 am)
Forget about datablocks for a minute. You'll need to learn about them (especially if you're targetting iTGB), but get an understanding of the basic mechanics of TorqueScript first.

You can have two types of functions in TorqueScript, those that stand alone:
function doSomething()
{
  //general stuff to do
}
Functions that stand alone are called without reference to any object:
doSomething();
An example of a function that is not linked to a namespace might be a math helper:
function addValues(%val1, %val2)
{
  return %val1 + %val2;
}

There are also functions those that are associated with a Namespace (think Class name or object name):
function ClassNameOrObjectName::doSomething(%this)
{
  //stuff to do on %this object
}
Functions that are associated with a namespace are called with reference to an object:
objectNameOrClass.doSomething(%this);
It's important to remember that functions that are linked to a namespace must have '%this' as the first parameter.

An example of a function that is linked to a namespace might be a shoot function:
function Player::Shoot(%this, %projectileType)
{
  %projectile = %this.%projectileType;
  ...
}
This just touches on it; my advice is to go through the tutorials to get a better understanding (like mine). :)

Good luck!
#7
11/03/2011 (10:24 am)
Don't count on me not knowing the basics just look at some of my custom code below and tell me i don't understand.

function createobject(%this)
{
%this.newsprite = new t2danimatedsprite(%this)
{
animationname = "triggerfish1sheetImageMapAnimation";
};
sceneWindow2D.getSceneGraph().addtoScene(%this.newsprite);
%this.setsize(15,15);
%this.setLayer(0);
%this.setvisible(true);
%this.setCollisionActive(true,true);
%this.class = fishclass;
schedule(3000,0,createobject77,fish77);
alxPlay(backgroundAudio);
fish.setBlendAlpha(1);
%this.config = testdatablock;
echo("created new object");
echo(%this.GetLayer());
echo(%this.getid());
}
datablock t2dSceneObjectDatablock(testdatablock)
{

position = "13.652 3.812";
};

for example if i run this and in the console window i type createobject(fish); it will create the object with the datablock testdatablock to give it it's position.

And your probably wondering do i understand what a datablock is.... yes its a static block of code that is a reference to all other objects that use it.

I understand vectors/packages/if then else statements/simobject/simsets/simgroups/arrays/for loops/whileloops/move maps/action maps/etc.... i'm not that big of a newb but am still a noob because of all the stuff i know but don't understand inheritance at all and when i tried to understand it, it just confused me and at my best efforts can't get it to work which lead to my twisted poor excuse of an example that i have posted here. so please try and explain how to inherit code from a datablock using the class and superclass with a function, with some working code please.
#8
11/03/2011 (10:31 am)
That is certainly some impressive code; you're well on your way. I don't know much about inheritance in TorqueScript, so I'll leave it to someone with a better knowledge base to help out.
#9
11/03/2011 (10:34 am)
Thanks i would appreciate the help.... i really need some.
#10
11/03/2011 (12:32 pm)
You should check out the thread on Parent/Child Datablocks.

A thread on datablock analogies might also help clear things up. Specifically, how to configure scene objects to use the datablocks.

Finally, check out this discussion on datablocks and classes. It explains how to call up the chain of inheritance.
#11
11/03/2011 (12:54 pm)
Thank you sooooo much ive been basically pulling hair at this point, i really appreciate the help.
#12
11/03/2011 (2:02 pm)
I hate to drag things on but whats wrong with this?

datablock t2dSceneObjectDatablock(ChickenDatablock : SpriteDatablock)
{
%life = "10";
};

datablock t2dSceneObjectDatablock(GooseDatablock : SpriteDatablock)
{
%life = "20";
};

datablock t2dSceneObjectDatablock(SpriteDatablock)
{


};

function testdummy(%this)
{
%this.config = SpriteDatablock;
echo(%life);
}


This is directly from the parent/child datablocks link that was given to me above with a few alterations that shouldn't stop the inheritance. I'm getting the impression that if i get it working that %Life will = 30 ... i hope i'm not going down a confusing path again.
the error message i'm getting is C:/Users/guy/Documents/MyGames/learning script/game/gameScripts/fish.cs Line: 52 - parse error
wow how embarrassing .... sigh what a day.
#13
11/03/2011 (2:55 pm)
There's a few problems I see:

1) The "SpriteDatablock" needs to be defined before the other two.
2) The "%this.config = SpriteDatablock;" won't work. That assignment only works when the object is created. You'll instead need to call "%this.setConfigDatablock(SpriteDatablock);". That function will copy the fields in the datablock into your object.
3) I'm not certain why you expect %Life to equal "30". If you made changes (1) and (2) above, %Life will equal "0" because it's never been assigned to that object.
#14
11/03/2011 (3:33 pm)
I need to take a quick break and ill make these changes asap when i return, thanks for baring with me on this one.
#15
11/03/2011 (5:28 pm)
I'm going to change my code and see if i can get something working.
#16
11/03/2011 (6:37 pm)
datablock t2dSceneObjectDatablock(ChickenDatablock : SpriteDatablock)
{
layer = 2;
};

datablock t2dSceneObjectDatablock(GooseDatablock : SpriteDatablock)
{
layer = 1;
};

datablock t2dSceneObjectDatablock(SpriteDatablock)
{
layer = 0;
};

function testdummy(%this)
{
%this.config = SpriteDatablock;
}

I have added a sceneobject using TGB and droped it into the scene with the name of testsceneobject and am calling it with the testdummy(testsceneobject).
This code works but still no inheritance.
So i'm going to do further tests to verify if i'm getting closer or further away from the answer.

well once again after countless testing i'm not really sure how close i am, i've even added a superclass and class to the following datablock.

datablock t2dSceneObjectDatablock(SpriteDatablock)
{
superclass = ChickenDatablock;
class = GooseDatablock;
layer = 0;
};
And still nothing i guess i'm doing something fundamentally wrong once again because from my understanding the layer should = 3 right?
Just like a noob to come back and ask a question that was thought to have been fully explained and understood.... sorry for the headache i know i've got to be one.
#17
11/04/2011 (7:56 am)
There's a few problems I see:

1) The "SpriteDatablock" needs to be defined before the other two. ChickenDatablock is derived off of SpriteDatablock, but it hasn't been defined yet!
2) The "%this.config = SpriteDatablock;" will not do anything. This assignment only works inside of the creation block. You'll instead need to call "%this.setConfigDatablock(SpriteDatablock);". That function will copy the fields in the datablock into your object.
3) I don't know why you'd expect the layer to equal 3. If you made changes (1) and (2) above, and your class used the SpriteDatablock, it would be on layer 0. If your class (correctly) used the ChickenDatablock, the layer would be 2.
#18
11/04/2011 (8:09 am)
Ok thanks i'm getting it, i'm done asking questions, Thanks again for leading me in the right direction and all the help here at the forums.
#19
11/21/2011 (6:29 am)
I decided to post another piece of code for noobs like me that learn best with working examples, so if this thread was confusing ill give an example of datablock inheritance to clear it up ..... and oh yea this is working code not that da** example code they always try to feed you which just confuses you more.


datablock t2dSceneObjectDatablock (test1)
{
position = "42.607 15.783";
};

datablock t2dSceneObjectDatablock (SpriteDatablock : test1)
{
layer = 3;
};

function createobject(%this)
{
%this.newsprite = new t2danimatedsprite(%this)
{
animationname = "triggerfish1sheetImageMapAnimation";
};
sceneWindow2D.getSceneGraph().addtoScene(%this.newsprite);
%this.setvisible(true);
%this.config = "spritedatablock";
}

Now all that's left to do is play the scene and open up the console window by pressing "~" and type createobject(test); and there you have it datablock inheritance.
#20
11/21/2011 (6:51 am)
Also one more thing ....... here's a working example of functions that are statically called to act as inheritance.


function inheriting::beingcalled(%this)
{
%this.setlayer(3);
}

function inherit::beingcalled(%this)
{
%this.setlinearvelocityx(1);
inheriting::beingcalled(%this);
}

function beingcalled(%this)
{
inherit::beingcalled(%this);
}

To test this out just drag a sprite object into the scene and name it test and save, then play scene and hit the "~" key and type beingcalled(test); and you will notice that it's layer is 3 and it is set with a linearvelocity of 1.