Advertisement

Inheritance in AngelScript

Started by January 28, 2009 09:02 AM
15 comments, last by WitchLord 15 years, 9 months ago
Revision 351 allows a class to inherit from itself and then goes into an infinite loop if you try to create an object of that class. Ex:
class C : C {}void main() {  C c;}

Also, virtual function calls don't seem to work with in references. Ex:
class A {  void print_me() {    print("a\n");  }};class B : A {  void print_me() {    print("b\n");  }}void print_a(A & in a) {  a.print_me();}void main() {  A a;  B b;  print_a(a);  print_a(b);}

It seems to work fine if print_a() is declared void print_a(A & inout a), void print_a(A @ a) or void print_a(A & a) just not void print_a(A & in a).


Big thanks for really testing this.

I'll have the self inheritance fixed as soon as possible.


The problem with in-references can't be fixed as easily, because this is a side effect of AngelScript having to take a copy of the object in order to guarantee that the original value isn't modified.

I may add a new behaviour for duplicating an object. AngelScript could then use that to make the copy rather than doing an assignment to the parameter type. Still, it will have to be for a future version as it is a rather large change.


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
You might also want to disable methods called "super" in classes. They seem to interact very strangely with constructors. For example:
class A {  A(int a) { this.a = a; }  int a;  void super() { print("A::super()"); }}class B : A {  B() { super(); }}void main() {  B b;}

This crashes the virtual machine, though in different places depending if AS_MAX_COMPATIBILITY is defined or not.
Yeah, I think I will just make the 'super' keyword reserved, at least when it comes to class methods. Allowing script writers to use this name for class methods will just confuse inexperienced programmers, when they try to call super() in constructors.

Anyway, I'll take a closer look at your example. That it crashes the VM indicates some serious problems, that may not be just because of the declaration of the super() method.

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

Quote: Original post by WitchLord
Yeah, I think I will just make the 'super' keyword reserved, at least when it comes to class methods. Allowing script writers to use this name for class methods will just confuse inexperienced programmers, when they try to call super() in constructors.


Damn! there goes my naming convention of super(), awesome() and notsosuper() for my AI routines!

(kidding)

Quote: Original post by SiCrane
You might also want to disable methods called "super" in classes. They seem to interact very strangely with constructors. For example:
class A {  A(int a) { this.a = a; }  int a;  void super() { print("A::super()"); }}class B : A {  B() { super(); }}void main() {  B b;}

This crashes the virtual machine, though in different places depending if AS_MAX_COMPATIBILITY is defined or not.


As I suspected, this was actually not caused by you declaring the method super(). Instead it happened because you didn't declare the default constructor in the class A. A bug in the compiler generated an incorrect default constructor which was the cause of the crash.

Quote: Original post by midnite
Damn! there goes my naming convention of super(), awesome() and notsosuper() for my AI routines!

(kidding)


[grin]

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
I've now implemented the scope resolution operator. It works for calling global functions from within class methods where the class implements a method with the same name as the function. It also works for calling methods from a base class where the derived class overrides the method.

Next I'm going to add support for the scope resolution operator when accessing variables, so that global variables can be accessed when a class has a property of the same name, or a function has a local variable of the same name.

The addition of the scope resolution operator also brings me one step closer to implementing support for namespaces.

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