| AngelScript |

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.

asIScriptEngine::RegisterObjectMethod
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.
asCALL_CDECL_OBJLAST
@ asCALL_CDECL_OBJLAST
A cdecl function that takes the object pointer as the last parameter.
Definition: angelscript.h:234
asFUNCTION
#define asFUNCTION(f)
Returns an asSFuncPtr representing the function specified by the name.
Definition: angelscript.h:675
asMETHOD
#define asMETHOD(c, m)
Returns an asSFuncPtr representing the class method specified by class and method name.
Definition: angelscript.h:740
asCALL_THISCALL
@ asCALL_THISCALL
A thiscall class method.
Definition: angelscript.h:232
asOFFSET
#define asOFFSET(s, m)
Returns the offset of an attribute in a struct.
Definition: angelscript.h:672