Auxiliary Interfaces

Registering an Interface

Class interfaces can be registered if you want to guarantee that script classes implement a specific set of class methods. Interfaces can be easier to use when working with script classes from the application, but they are not necessary as the application can easily enumerate available methods and properties even without the interfaces.

asbind20::interface(engine, "my_interface")
    // Declarations only
    .method("int get() const")
    .funcdef("int callback(int)")
    .method("int invoke(callback@ cb) const");

Note

Unlike the raw AngelScript interface, you don’t need to add the class name into the declaration of member funcdef for asbind20.

Type Aliases

Function definitions can be registered when you wish to allow the script to pass function pointers to the application, e.g. to implement callback routines.

Enumeration types and typedefs can also be registered to improve readability of the scripts.

inline global &funcdef(std::string_view decl)

Register a funcdef.

Parameters:

decl – Function declaration

inline global &typedef_(std::string_view type_decl, std::string_view new_name)

Register a typedef.

Parameters:
  • type_decl – Type declaration

  • new_name – Aliased type name

inline global &using_(std::string_view new_name, std::string_view type_decl)

Register a typedef in C++11 style.

Parameters:
  • new_name – Aliased type name

  • type_decl – Type declaration

Example code:

asbind20::global(engine)
    .funcdef("bool callback(int, int)")
    .typedef_("float", "real32")
    // For those who feel more comfortable with the C++11 style
    // "using alias = type;"
    .using_("float32", "float");

Enumerations

enum class my_enum : int
{
    A,
    B
};
enum_<my_enum>(engine, "my_enum")
    .value(my_enum::A, "A")
    .value(my_enum::B, "B");

Note

Until the version 2.39, AngelScript uses 32-bit integer to store underlying value of enums. before it supports customizable underlying type. Please make sure those values don’t overflow or underflow.

Besides, the library provides a convenient interface for generating string representation of enum value at compile-time.

The following code is equivalent to the above one:

asbind20::enum_<my_enum>(engine, "my_enum")
    .value<my_enum::A>()
    .value<my_enum::B>();

Note

However, as static reflection is still waiting for the C++26, this feature relies on compiler extension and is platform dependent. It has some limitations. For example, it cannot generate string representation for enums with same value.

enum overlapped
{
    A = 1,
    B = 1 // Not supported for this kind of enum value
};

If you are interested in how this is achieved, you can read this article written by YKIKO (Chinese), or author’s English translation.

Since the version 2.39, AngelScript supports enumerations with custom underlying types.

You can register them by enum_underlying, which is an alias of enum_<Enum, std::underlying_type_t<Enum>>.

enum class enum_uint64 : std::uint64_t
{
    A,
    B = std::uint64_t(-1) // Larger than UINT32_MAX
};
asbind20::enum_underlying<enum_uint64>(engine, "enum_uint64")
    .value(enum_uint64::A, "A")
    .value(enum_uint64::B, "B");

Note

If you need to interact these enums from C++ side, the enums with custom underlying type need to specify the conversion rules.