| AngelScript |

Registering object properties

Class member variables can be registered so that they can be directly accessed by the script without the need for any method calls.

struct MyStruct
{
int a;
};
r = engine->RegisterObjectProperty("mytype", "int a", asOFFSET(MyStruct,a)); assert( r >= 0 );

If a class member is indirect, i.e. the class holds a pointer to the member that is allocated on the heap, then it is possible to registered the property using & to tell the script engine that an dereference is needed to access the member.

struct MyStruct
{
OtherStruct *a;
};
r = engine->RegisterObjectProperty("mytype", "othertype &a", asOFFSET(MyStruct,a)); assert( r >= 0 );

Of course, the application must make sure the pointer is valid during the whole time that it may be accessed from the script.

# Composite members

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

struct Component
{
int a;
};
struct Object
{
Component *comp;
};
r = engine->RegisterObjectProperty("object", "comp_a", asOFFSET(Component, a), asOFFSET(Object, comp), true); assert( r >= 0 );

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

# Property accessors

It is also possible to expose properties through property accessors, which are a pair of class methods with prefixes 'get_' and 'set_' and the function decorator 'property' for getting and setting the property value. These methods should be registered with RegisterObjectMethod. This is especially useful when the offset of the property cannot be determined, or if the type of the property is not registered in the script and some translation must occur, i.e. from `char*` to `string`.

If the application class contains a C++ array as a member, it may be advantageous to expose the array through indexed property accessors rather than attempting to matching the C++ array type to a registered type in AngelScript. To do this you can create a couple of simple proxy functions that will translate to the array access.

Note
  The behaviour of virtual properties can be customized with the engine property asEP_PROPERTY_ACCESSOR_MODE.

struct MyStruct
{
int array[16];
};
// Write a couple of proxy
int MyStruct_get_array(unsigned int idx, MyStruct *o)
{
if( idx >= 16 ) return 0;
return o->array[idx];
}
void MyStruct_set_array(unsigned int idx, int value, MyStruct *o)
{
if( idx >= 16 ) return;
o->array[idx] = value;
}
// Register the proxy functions as member methods
r = engine->RegisterObjectMethod("mytype", "int get_array(uint) property", asFUNCTION(MyStruct_get_array), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("mytype", "void set_array(uint, int) property", asFUNCTION(MyStruct_set_array), asCALL_CDECL_OBJLAST); assert( r >= 0 );
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
asIScriptEngine::RegisterObjectProperty
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.
asOFFSET
#define asOFFSET(s, m)
Returns the offset of an attribute in a struct.
Definition: angelscript.h:672