AngelScript cannot automatically determine relationships between registered classes, so in order to establish the hierarchies for use within the script language it is necessary to do a bit more registration beyond the normal object registration.
Hierarchies can currently only be registered for reference types, not for value types.
Establishing the relationship
In order to let AngelScript know that two types are related you need to register the reference cast operators opCast and opImplCast. The opCast should be used if you only want to allow the cast through an explicit call with the cast<class>
operator. opImplCast should be used when you want to allow the compiler to implicitly perform the cast as necessary.
Usually you'll want to use opImplCast for casts from a derived type to the base type, and opCast for casts from a base type to a derived type.
template<class A, class B>
B* refCast(A* a)
{
if( !a ) return 0;
B* b = dynamic_cast<B*>(a);
if( b != 0 )
{
b->addref();
}
return b;
}
r = engine->RegisterObjectMethod(
"base",
"const derived@ opCast() const",
asFUNCTION((refCast<base,derived>)),
asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod(
"derived",
"const base@ opImplCast() const",
asFUNCTION((refCast<derived,base>)),
asCALL_CDECL_OBJLAST); assert( r >= 0 );
@ asCALL_CDECL_OBJLAST
A cdecl function that takes the object pointer as the last parameter.
Definition: angelscript.h:234
#define asFUNCTION(f)
Returns an asSFuncPtr representing the function specified by the name.
Definition: angelscript.h:673
Note that it may be necessary to add extra parenthesis to the asFUNCTION
macro so that the preprocessor doesn't interpret the ,
in the template declaration as the argument separator in the macro.
Inherited methods and properties
Just as relationships cannot be determined, there is also no way to automatically let AngelScript add inherited methods and properties to derived types. The reason for this is that method pointers and property offsets may differ between the base class and derived class, especially when multiple inheritance is used, and there is no way to automatically determine exactly what the difference is.
For this reason the application needs to register all the inherited methods and properties for the derived classes, which may lead to a bit of duplicate code. However, you may be able to avoid the duplication through a bit of clever thinking. Here is an example of registering the methods and properties for a base class and the derived class (registration of behaviours has been omitted for briefness):
class base
{
public:
virtual void aMethod();
int aProperty;
};
class derived : public base
{
public:
virtual void aNewMethod();
int aNewProperty;
};
template <class T>
{
int r;
}
template <class T>
{
int r;
RegisterBaseMembers<T>(engine, type);
}
{
int r;
RegisterBaseMembers<base>(engine, "base");
RegisterDerivedMembers<derived>(engine, "derived");
}
@ asCALL_THISCALL
A thiscall class method.
Definition: angelscript.h:232
#define asOFFSET(s, m)
Returns the offset of an attribute in a struct.
Definition: angelscript.h:670
#define asMETHOD(c, m)
Returns an asSFuncPtr representing the class method specified by class and method name.
Definition: angelscript.h:738
@ asOBJ_REF
A reference type.
Definition: angelscript.h:250
The engine interface.
Definition: angelscript.h:1061
virtual int RegisterObjectProperty(const char *obj, const char *declaration, int byteOffset, int compositeOffset=0, bool isCompositeIndirect=false)=0
Registers a property for the object type.
virtual int RegisterObjectType(const char *obj, int byteSize, asDWORD flags)=0
Registers a new object type.
virtual int RegisterObjectMethod(const char *obj, const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv, void *auxiliary=0, int compositeOffset=0, bool isCompositeIndirect=false)=0
Registers a method for the object type.