🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Inheritence

Started by
5 comments, last by WitchLord 18 years, 2 months ago
I've played a little bit with AS code, and was able to modify it to support inheritence from other classes (for now: only single inheritence). Here are the steps: - (as_objecttype.h) asCObjectType:

asCObjectType* parent;

- (as_scriptengine.h) asCScriptEngine (also modify angelscript.h):

RegisterObjectType(const char *objname, int byteSize, asDWORD flags, const char *objparent);

- (as_scriptengine.cpp) asCScriptEngine::RegisterObjectType:

   asCObjectType *parent = 0;
   for( n = 0; n < objectTypes.GetLength(); n++ )
   {
      if( objectTypes[n]->name == name )
         return asALREADY_REGISTERED;
      if ( objparent != 0 && objectTypes[n]->name == objparent )
         parent = objectTypes[n];
   }
   for( n = 0; n < arrayTypes.GetLength(); n++ )
   {
      if( arrayTypes[n]->name == name )
         return asALREADY_REGISTERED;
      if ( objparent != 0 && arrayTypes[n]->name == objparent )
         parent = arrayTypes[n];
   }
   if ( objparent !=0 && parent == 0)
      return ConfigError(asINVALID_NAME);

   .
   .
   .
   type->parent = parent;


- (as_builder.cpp) GetObjectProperty

asCProperty *asCBuilder::GetObjectProperty(asCDataType &obj, const char *prop)
{
   assert(obj.GetObjectType() >= 0);

   // TODO: Only search in config groups to which the module has access
   // TODO: Improve linear search
   for ( asCObjectType *object = obj.GetObjectType(); object != 0; object = object->parent )
   {
      asCArray<asCProperty *> &props = object->properties;
      for( asUINT n = 0; n < props.GetLength(); n++ )
         if( props[n]->name == prop )
            return props[n];
   }

   return 0;
}


- (as_builder.cpp) GetObjectMethodDescriptions Add:

   if ( objectType->parent != 0 )
      GetObjectMethodDescriptions(name, objectType->parent, methods, objIsConst);


Please add your comments...
Advertisement
Not bad. It looks like it would work well.

But what about overloaded methods and properties? And what about virtual methods? And casting between base class and sub classes?

There is a reason I haven't added support for inheritance myself yet. It just opens up too many what-ifs that I don't want to go into yet.

I thank you for your effort and your contribution anyway, but I will not add it to the official library just yet. I'll keep it though, for when I'm ready to finally add this support.

Thanks,
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

I had to support basic windowing (button which inherits from a window for example), and it works well.

I know there are other issues as well, but it's better than nothing :)

What I miss in the library, is the inheritence option, and GET/PUT properties for a class. After adding that - I think it would be perfect ;)

Gilad
Hmm... when I think about it:

Virtual functions should work without any problem. Since you call the current pointer in the virtual map, I don't think there should be any problem (it would always call the latest registered function)

Regarding overloaded functions, it should work too, since GetObjectProperty would try to search the current object first, and then go to the parent.

Casting: maybe we need to pass a difference value from the parent. not sure.

[Edited by - gilad_novik on May 4, 2006 5:51:28 PM]
Yes, virtual methods should work, though if the method was registered for both the base class and the sub class then the compiler would have to be able to choose the right one. The same goes for overloaded methods (without being virtual).

The properties probably would work correctly as it is now, for the very reason you mentioned.

I'm finishing up the interface feature for AngelScript (I'll soon release the first WIP with it). Once that is finished I'll start working on inheritance, then I should be able to use your code.

The Get/Put properties are a more complicated matter which require extensive work with the compiler so this will have to wait a while more.

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

Get/Put properties:

Just to make sure we are talking about the same thing: property for an object - instead of accessing the variable, simply call 2 functions (Get/Put).

I think that the easiest way to implement it, is to register the methods using a special function. When the compiler tries to look for a registered variable, it should fall back to those functions, and simply replace the generated byte code with normal functions calls.

Did I miss something? If not, I'll try to play with it soon, and post my comments when I'm done.

Gilad
Yes, we are talking about the same thing.

And by all means, feel free to implement it. It would be a great contribution.

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