Inheritance in AngelScript

Started by
15 comments, last by WitchLord 15 years, 2 months ago
Hi everybody, I've begun the implementation of inheritance in AngelScript. I know a lot of people have been waiting for this feature so I thought I'd let you guys know the good news. I've actually already checked in a good part of the implementation in the SVN, as work-in-progress for version 2.15.2. It would be good if those of you who plan on using the feature, could start testing it already so that I can work out the kinks even before the final release of 2.15.2 (probably by the end of February). Things that is already working: - single inheritance, where the derived class inherits properties and methods from the parent - overriding inherited methods - polymorphing through the use of virtual methods (all class methods are by default virtual) - implicit cast from derived class to base class - explicit dynamic cast from base class to derived class - save/load bytecode that includes class inheritance Things that still needs to be implemented: - Base class' destructor must be called upon destroying a derived class - Calling the base class' constructor from within the constructor of the derived class - Calling base class' method from within overridden method in derived class Things that won't be implemented (at least not at this moment): - multiple inheritance - inheritance from application registered classes - initialization lists for constructors - non-virtual class methods Please let me know your suggestions and concerns. 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

Advertisement
Wow, that's awesome. I've really waited for that. I used a lot of inheritance in my c++ code last year and dreamed of being able to use inheritance in angelscript as well :)

Just too make shure, I will be able to register classes and their base classes with angelscript and cast instances of those in a script? Can't wait for it.
AngelScript already supports registering class hierarchies with proper casting between base classes and derived classes. You establish the available paths for the casting by registering the asBEHAVE_REFCAST behaviour.

This has actually been available since version 2.13.0, which was released in June last year. :)

What I'm implementing now is inheritance for classes declared in the scripts.

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

It's a bit surprising that you can't use a derived object with a base reference. Ex:
class A {  void print_me() { print("A\n"); }}class B : A {  void print_me() { print("B\n"); }}void foo(A & in a) {  a.print_me();}void main() {  A a;  B b;  foo(a);  foo(b); // generates a "No matching signatures to 'foo(B&)'"}
WitchLord you are the man!

It's been a few weeks since our very long discussion about persistent script objects, and I have been grinding away at other parts of my project that needed attention.

With them done I came back to revisit my scripting implementation, and low and behold, you implemented the one thing I've been needing!

I will check out the latest SVN when I get home and begin messing with this :) :)
==============================
A Developers Blog | Dark Rock Studios - My Site
Quote:Original post by SiCrane
It's a bit surprising that you can't use a derived object with a base reference. Ex:


That is indeed surprising. :)

I'll look into this. Thanks for letting me know.

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

Sounds cool :).
regards.
If you say "pls", because it is shorter than "please", I will say "no", because it is shorter than "yes"
http://nightlight2d.de/
Progress is continuing...

I've fixed the problem that SiCrane mentioned, and I've implemented the call of the base class' constructor and destructor.

The base class' constructor is called from within a constructor via the keyword super, like this:

class Derived : Base{  Derived(int param)  {    if( param > 0 )      super(4);    else      super();  }}


As you can see the call to the base class' constructor can be made in if-statements, allowing you more flexibility in how you initialize the classes.

If the base class' constructor is not manually called, the compiler will automatically insert a call to the base class' default constructor at the beginning of the constructor.

The last major thing that I need to implement before releasing 2.15.2 is the scoping operator, so that base classes methods can be called from overloaded methods in the derived 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

So if you had this code:
class Derived : Base{  Derived(int param)  {    if( param > 0 )      super(4);  }}

would the base constructor just not be called if param is 0?
You get a compiler error if not all paths call the constructor. Likewise, you get a compiler error if you attempt to call the constructor multiple times, or in a loop.

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