Global Functions and Properties

Registering Global Functions

Ordinary Functions

C++ Declarations:

void func(int arg);

void gfn(asIScriptGeneric* gen);
void gfn_using_aux(asIScriptGeneric* gen);

Registering:

asbind20::global(engine)
    // Ordinary function (native)
    .function("void func(int arg)", &func)
    // Ordinary function (generic)
    .function("void gfn(int arg)", &gfn)
    .function("void gfn(int arg)", &gfn_using_aux, asbind20::auxiliary(/* some auxiliary data */));

Note

Make sure the script function declaration matches what the registered function does with the asIScriptGeneric!

The binding generator will automatically distinguish between stdcall and cdecl calling convention, if they are actually different, e.g., when compiled on x86 Windows.

For overloaded functions, you need to use overload_cast with arguments to choose the function you want.

void func(int, int);
void func(float);
using namespace asbind20;

global(engine)
    .function("void func(int, int)", overload_cast<int, int>(&func))
    .function("void func(float)", overload_cast<float>(&func));

Member Function as Global Function

You can synthesize global function by member function and an instance:

class my_class
{
    int f();
};
my_class instance{};

asbind20::global(engine)
    .function("int f()", &my_class::f, asbind20::auxiliary(instance));

When the f() is called by script, it’s equivalent to instance.f() in C++.

Registering Global Properties

int global_var = 42;
const int const_global_var = 42;
asbind20::global(engine)
    .property("int global_var", global_var)
    .property("const int const_global_var", const_global_var);

Special Functions

Please check the official documentation of AngelScript for the requirements of following functions.

Message Callback

Registered by message_callback.

global &message_callback(asGENFUNC_t gfn, void *obj = nullptr) = delete

Generic calling convention for message callback is not supported.

template<native_function Callback>
inline global &message_callback(Callback fn, void *obj = nullptr)

Set the message callback.

template<native_function Callback, typename T>
inline global &message_callback(Callback fn, T &obj)

Set a member function as the message callback.

See AngelScript documentation for details.

Exception Translator

Registered by exception_translator.

global &exception_translator(asGENFUNC_t gfn, void *obj = nullptr) = delete

Generic calling convention for exception translator is not supported.

template<native_function Callback>
inline global &exception_translator(Callback fn, void *obj = nullptr)

Set the exception translator.

template<native_function Callback, typename T>
inline global &exception_translator(Callback fn, T &obj)

Set a member function as the exception translator.

Note

If your AngelScript is built without exception support (asGetLibraryOptions() reports AS_NO_EXCEPTIONS), this helper will fail to register the translator.

See AngelScript documentation about C++ exceptions for details.