Advertisement

Child class with same variable

Started by August 11, 2012 07:59 PM
1 comment, last by WitchLord 12 years, 4 months ago
I've run into a problem with inheritance in AngelScript. I finally got around to implementing parent objects into my engine, so I thought I would use simple polymorphism to keep from copying and pasting the parent's code into the child's code.

A basic object in my game would look like this:

class objActor_controller
{
Object @object;
float x;
float y;

int animation;
int[] animation_begin;
int[] animation_end;

objActor_controller(Object @obj)
{
@this.object = obj;
x = object.x;
y = object.y;
}

void Create()
{
//Does some initial setup
}
}


Lets say I decide to write a child class:

class objPlayer_controller : objActor_controller
{
Object @object;
float x;
float y;
Child_controller(Object @obj)
{
@this.object = obj;
x = object.x;
y = object.y;
}
void Create()
{
objActor_controller::Create();
//Sets up animations specific for this character...
}

void Step()
{
x += 1;
y += 1;
}
}


Using the method above makes the compiler complain about name conflicts with x, y and @object since my game editor automatically adds these. Since the C++ equivilent allows for it, Shouldn't AngelScript allow this too?
Out of curiosity though, why are you doing that?
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Advertisement
When objPlayer_controller derives from objActor_controller it already receives the object, x, and y members. That's why you can't add them again.

C++ accepts adding them again, but you actually end up with two sets of members, i.e. the methods from the base class is not working on the same members as the methods from the derived class.

You need to modify the game editor to avoid adding members that are already inherited from the base class.

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement