Game Development Community

Inheritance Problems

by Caleb · in Technical Issues · 01/13/2008 (6:50 am) · 7 replies

Using the starter.fps kit, I have made three new datablocks that inherit from PlayerBody. They are LightArmor, MediumArmor, and HeavyArmor. They inherit everything but movement (shapes will come later), so that players can choose if they'd rather be weak but fast, or slow but strong.

Anyways, everything works fine, except that "Armor" function don't work on these datablocks. All the functions such as "Armor::onAdd", "Armor::onCollision", and "Armor::damage", don't work. I've tried putting in "className = Armor;" in each, but it doesn't help.

So. . . . .
Why is this happening?
How might I fix this?
Will they not inherit the same class name?
Will I have to make new functions for each new datablock?

Thanks for any help you can give me.

#1
01/14/2008 (9:51 am)
It's a bit of a pain, the script inheritance doesn't quite work the way you might expect it to.

You will have to create a new version of each of the Armor:: functions for your new classes so you'll need a LightArmor::onAdd, a mediumArmor::onAdd etc etc etc.

You can just copy and paste them from player.cs, and do a quick search and replace.

If you are certain that all your somethingArmor classes are going to do the same things as the Armor classes, I think you could strip the code in the new functions and replace it with a call to Armor::someFunction like this

function MediumArmor::onRemove(%this, %obj)
{
   Armor::onRemove(%obj);
}
#2
01/14/2008 (6:00 pm)
Alrighty then. Thanks for then info.

There are a great many functions that are required for all the armors, so I'll most likely change the namespaces to "Player". Either that, or do some major in-game alteration after the player chooses their armor.
#3
01/14/2008 (7:36 pm)
When you define the inherited datablocks you need to specify the className. This (I think) is what allows the inherited methods to work in the sub-class.

With the code listed below I can call all the methods for SwingDoor for objects created with the inherited datablocks.


//-------------------------------------------------
//This is the base class datablock for swing doors
datablock StaticShapeData(SwingDoor)
{
	category = "Door";			
};

//-------------------------------------------------
// Data block for wooden swing door with round knob and lock
datablock StaticShapeData(WoodenInternalDoor:SwingDoor)
{
	shapeFile = "~/data/shapes/door/woodeninternaldoor.dts";	
	className = "SwingDoor";
};

//-------------------------------------------------
// Data block for windowed white internal door
datablock StaticShapeData(WindowedWhiteInternalDoor:SwingDoor)
{
	shapeFile = "~/data/shapes/door/windoweddoorwhite.dts";	
	className = "SwingDoor";
};
#4
01/15/2008 (1:09 am)
The Namespace Upgrade Patch provides this kind of inheritance. I've been using it for quite some time now.

It also provides the Parent:: reference in script.
#5
01/15/2008 (5:44 am)
Sweet! I'll look at this later this afternoon.

Thanks!
#6
01/18/2008 (3:02 am)
Could you tell me why do you use SwingDoor twice in this code
datablock StaticShapeData(WindowedWhiteInternalDoor:SwingDoor)
{
shapeFile = "~/data/shapes/door/windoweddoorwhite.dts";
className = "SwingDoor";
};
#7
01/22/2008 (5:59 am)
Well, just in case anyone was wondering, the inheritance works fine.

I used radio buttons in the armor selection menu, but I had the buttons named "LightArmor", "MediumArmor", and "HeavyArmor", which is exactly what I named my player DataBlocks. Some renaming, and everything works great! :D