| AngelScript |

All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Registering object methods

Class methods are registered with the RegisterObjectMethod call. Both non-virtual and virtual methods are registered the same way.

Static class methods are in reality global functions so those should be registered as global functions and not as object methods.

// Register a class method
void MyClass::ClassMethod()
{
// Do something
}
r = engine->RegisterObjectMethod("mytype", "void ClassMethod()", asMETHOD(MyClass,ClassMethod), asCALL_THISCALL); assert( r >= 0 );

It is also possible to register a global function that takes a pointer to the object as a class method. This can be used to extend the functionality of a class when accessed via AngelScript, without actually changing the C++ implementation of the class.

// Register a global function as an object method
void MyClass_MethodWrapper(MyClass *obj)
{
// Access the object
obj->DoSomething();
}
r = engine->RegisterObjectMethod("mytype", "void MethodWrapper()", asFUNCTION(MyClass_MethodWrapper), asCALL_CDECL_OBJLAST); assert( r >= 0 );

See Also
  Registering a function for more details on how the macros work.

# Composite members

If the application class that is being registered uses composition, then it is possible to register the methods of the composite members like this:

struct Component
{
int DoSomething();
};
struct Object
{
Component *comp;
};
r = engine->RegisterObjectMethod("object", "int DoSomething()", asMETHOD(Component, DoSomething), asCALL_THISCALL, 0, asOFFSET(Object, comp), true); assert( r >= 0 );

The last parameter indicates that to reach the composite member it is necessary to dereference the pointer. If the composite member is inlined, then the parameter should be set to false.