Game Development Community

Question about inheritence

by Matt "Mr. Pig" Razza · in Torque Game Engine · 05/26/2007 (2:36 pm) · 5 replies

I'm wondering if it would be possible for ShapeBase to use it's child class Player. There are some functions in ShapeBase I want to edit dynamically based on a datablock tracked within the Player class. I'd rather not do some "hacked together" method of passing values of the data, or even the datablock, to Parent:: mainly because the datablock is not ShapeBaseData derived and non-player Player classes won't know what to do with it.

Any insight would be nice, if worse does come to worse I'll guess I'll just pass the pointer to Parent:: but I'd rather not.

Thanks.

#1
05/26/2007 (3:05 pm)
I believe you can use a dynamic_cast:

Player* plr = dynamic_cast<Player*>(this);
if(!plr)
     return;

Although the best thing might be to move the functionality into the player class directly. You can overload a shapebase function to put code in for the player.

Hope that help.
#2
05/26/2007 (6:49 pm)
Yeah, I guess the best way would be an overload because you wouldn't be able to tell whether or not it's child is a Player class or something else.
#3
05/30/2007 (2:57 pm)
Just tested it, turns out you can't overload the function (at least not as Player:: which is the only useful method) and the Player Dynamic Cast causes some major friend-class problems as ShapeBase can't access the required vars within plr and even if it could it would not be able to access the vars within the var of plr.

Any other ideas?
#4
05/30/2007 (3:24 pm)
Why can't you overload it?

virtual void ShapeBase::DoStuff(){}
void Player::DoStuff(){// code here}

ShapeBase *sb = GetShapeBasePointer();
sb->DoStuff(); // will call your player->DoStuff() if indeed this is a player.
#5
06/01/2007 (12:59 pm)
error C3254: 'Player' : class contains explicit override 'setDamageLevel' but does not derive from an interface that contains the function declaration

or

error C2723: 'ShapeBase::setDamageLevel' : 'virtual' storage-class specifier illegal on function definition

No idea why it's doing that.