Invoking Script Interfaces

Invoking a Script Function

This library can automatically convert arguments in C++ for invoking an AngelScript function.

template<typename R, typename ...Args>
script_invoke_result<R> asbind20::script_invoke(asIScriptContext *ctx, asIScriptFunction *func, Args&&... args)

Call a script function.

This example uses the string extension from asbind20 as an example, you can change the std::string to your underlying string type.

AngelScript function:

string test(int a, int&out b)
{
    b = a + 1;
    return "test";
}

C++ code:

asIScriptEngine* engine = /* Get a script engine */;
asIScriptModule* m = /* Build the above script */;
asIScriptFunction* func = m->GetFunctionByName("test");
if(!func)
    /* Error handling */

// Manage script context using the RAII helper
asbind20::request_context ctx(engine);

int val = 0;
auto result = asbind20::script_invoke<std::string>(
    ctx, func, 1, std::ref(val) // The reference of primitive type needs a wrapper
);

assert(result.value() == "test");
assert(val == 2);

Using a Script Class

The library provides tools for instantiating a script class.

inline script_object asbind20::instantiate_class(asIScriptContext *ctx, const asITypeInfo *class_info)

Instantiate a script class using its default factory function.

Note

This function requires the class to be default constructible

Parameters:
  • ctx – Script context

  • class_info – Script class type information

Returns:

Instantiated script object, or empty object if failed

The script_invoke also supports invoking a method, a.k.a., member function. You need to put the script object in front of the script function in arguments, this is designed to simulate a method call obj.method().

template<typename R, script_object_handle Object, typename ...Args>
script_invoke_result<R> asbind20::script_invoke(asIScriptContext *ctx, Object &&obj, asIScriptFunction *func, Args&&... args)

Call a method on script object.

The script class defined in AngelScript:

class my_class
{
    int m_val;

    void set_val(int new_val) { m_val = new_val; }
    int get_val() const { return m_val; }
    int& get_val_ref() { return m_val; }
};

C++ code:

asIScriptEngine* engine = /* Get a script engine */;
asIScriptModule* m = /* Build the above script */;
asITypeInfo* my_class_t = m->GetTypeInfoByName("my_class");

asbind20::request_context ctx(engine);

auto my_class = asbind20::instantiate_class(ctx, my_class_t);

asIScriptFunction* set_val = my_class_t->GetMethodByDecl("void set_val(int)");
asbind20::script_invoke<void>(ctx, my_class, set_val, 182375);

asIScriptFunction* get_val = my_class_t->GetMethodByDecl("int get_val() const");
auto val = asbind20::script_invoke<int>(ctx, my_class, get_val);

assert(val.value() == 182375);

asIScriptFunction* get_val_ref = my_class_t->GetMethodByDecl("int& get_val_ref()");
auto val_ref = asbind20::script_invoke<int&>(ctx, my_class, get_val_ref);

assert(val_ref.value() == 182375);

*val_ref = 182376;

val = asbind20::script_invoke<int>(ctx, my_class, get_val);
assert(val.value() == 182376);

Reference of Invocation Tools

template<typename R>
auto asbind20::get_context_result(asIScriptContext *ctx)

Get the result of context.

Template Parameters:

R – Return type. It can be safely ignored by void if you only want the error code

Parameters:

ctx – Script context. Cannot be nullptr.

Returns:

Result of the execution

The result types of script invocation consist of the primary template, specialization for references, and specialization for void.

template<typename T>
class script_invoke_result : public asbind20::script_invoke_result_base

Script invocation result.

Template Parameters:

T – Result type

Unchecked Accessors

Note

Please check the status of object before directly accessing the value!

inline return_type operator*() const
inline pointer_type operator->() const

Note

This function is only available if return type is reference or convertible to pointer

Checked Accessors

Throws an exception when the object does not contain a returned value

inline return_type value() const
template<typename U = std::remove_cv_t<T>>
inline value_type value_or(U &&default_val) const

Public Types

using value_type = T
using return_type = decltype(get_script_return<T>(std::declval<asIScriptContext*>()))
using pointer_type = typename detail::invoke_result_traits<return_type>::pointer_type

Public Functions

script_invoke_result(const script_invoke_result&) noexcept = default
script_invoke_result &operator=(const script_invoke_result &other) noexcept = default
~script_invoke_result() = default
inline std::optional<T> to_optional() const
inline explicit operator std::optional<T>() const
template<typename T>
class script_invoke_result<T&> : public asbind20::script_invoke_result_base

Script invocation result for references.

Public Types

using value_type = T&
using return_type = T&
using pointer_type = T*

Public Functions

~script_invoke_result() = default
script_invoke_result &operator=(const script_invoke_result &other) noexcept = default
inline return_type operator*() const noexcept
inline pointer_type operator->() const noexcept
inline return_type value() const
template<typename U = std::remove_cv_t<T>>
inline T &value_or(U &&default_val) const
template<>
class script_invoke_result<void> : public asbind20::script_invoke_result_base

Script invocation result for void type.

Public Types

using value_type = void
using return_type = void

Public Functions

script_invoke_result(const script_invoke_result &other) noexcept = default
~script_invoke_result() = default
script_invoke_result &operator=(const script_invoke_result &other) noexcept = default
inline void operator*() const noexcept
inline void value() const