Skip to content

Commit

Permalink
Register and call virtual methods on scripts in GDExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
limbonaut committed Jun 30, 2024
1 parent 5e89a82 commit cd0bc8e
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 97 deletions.
2 changes: 1 addition & 1 deletion bt/bt_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void BTState::_exit() {
}

void BTState::_update(double p_delta) {
VCALL_ARGS(_update, p_delta);
GDVIRTUAL_CALL(_update, p_delta);
if (!is_active()) {
// Bail out if a transition happened in the meantime.
return;
Expand Down
31 changes: 18 additions & 13 deletions bt/tasks/bt_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ String BTTask::get_task_name() {
ERR_FAIL_COND_V_MSG(has_generate_method && !task_script->is_tool(), _generate_name(), vformat("BTTask: @tool annotation is required if _generate_name is defined: %s", task_script->get_path()));
if (task_script->is_tool() && has_generate_method) {
String call_result;
VCALL_V(_generate_name, call_result);
GDVIRTUAL_CALL(_generate_name, call_result);
if (call_result.is_empty() || call_result == "<null>") {
// Force reset script instance.
set_script(Variant());
set_script(task_script);
// Retry.
VCALL_V(_generate_name, call_result);
GDVIRTUAL_CALL(_generate_name, call_result);
}
ERR_FAIL_COND_V_MSG(call_result.is_empty() || call_result == "<null>", _generate_name(), vformat("BTTask: _generate_name() failed to return a proper name string (%s)", task_script->get_path()));
return call_result;
Expand Down Expand Up @@ -171,7 +171,9 @@ void BTTask::initialize(Node *p_agent, const Ref<Blackboard> &p_blackboard, Node
get_child(i)->initialize(p_agent, p_blackboard, p_scene_root);
}

VCALL_OR_NATIVE(_setup);
if (!GDVIRTUAL_CALL(_setup)) {
_setup();
}
}

Ref<BTTask> BTTask::clone() const {
Expand Down Expand Up @@ -245,16 +247,21 @@ BT::Status BTTask::execute(double p_delta) {
data.children.get(i)->abort();
}
}

VCALL_OR_NATIVE(_enter);
if (!GDVIRTUAL_CALL(_enter)) {
_enter();
}
} else {
data.elapsed += p_delta;
}

VCALL_OR_NATIVE_ARGS_V(_tick, Status, data.status, p_delta);
if (!GDVIRTUAL_CALL(_tick, p_delta, data.status)) {
data.status = _tick(p_delta);
}

if (data.status != RUNNING) {
VCALL_OR_NATIVE(_exit);
if (!GDVIRTUAL_CALL(_exit)) {
_exit();
}
data.elapsed = 0.0;
}
return data.status;
Expand All @@ -265,7 +272,9 @@ void BTTask::abort() {
get_child(i)->abort();
}
if (data.status == RUNNING) {
VCALL_OR_NATIVE(_exit);
if (!GDVIRTUAL_CALL(_exit)) {
_exit();
}
}
data.status = FRESH;
data.elapsed = 0.0;
Expand Down Expand Up @@ -356,7 +365,7 @@ PackedStringArray BTTask::get_configuration_warnings() {
PackedStringArray warnings;
Ref<Script> task_script = get_script();
if (task_script.is_valid() && task_script->is_tool()) {
VCALL_V(_get_configuration_warnings, warnings); // Get script warnings.
GDVIRTUAL_CALL(_get_configuration_warnings, warnings); // Get script warnings.
}
ret.append_array(warnings);
ret.append_array(_get_configuration_warnings());
Expand Down Expand Up @@ -439,16 +448,12 @@ void BTTask::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::INT, "status", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_status");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "elapsed_time", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_elapsed_time");

#ifdef LIMBOAI_MODULE
GDVIRTUAL_BIND(_setup);
GDVIRTUAL_BIND(_enter);
GDVIRTUAL_BIND(_exit);
GDVIRTUAL_BIND(_tick, "delta");
GDVIRTUAL_BIND(_generate_name);
GDVIRTUAL_BIND(_get_configuration_warnings);
#elif LIMBOAI_GDEXTENSION
// TODO: Registering virtual functions is not available in godot-cpp...
#endif
}

BTTask::BTTask() {
Expand Down
3 changes: 1 addition & 2 deletions bt/tasks/bt_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#ifdef LIMBOAI_GDEXTENSION
#include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/classes/resource.hpp>
#include <godot_cpp/core/gdvirtual.gen.inc>
#include <godot_cpp/core/object.hpp>
#include <godot_cpp/templates/vector.hpp>
using namespace godot;
Expand Down Expand Up @@ -103,14 +104,12 @@ class BTTask : public BT {
virtual void _exit() {}
virtual Status _tick(double p_delta) { return FAILURE; }

#ifdef LIMBOAI_MODULE
GDVIRTUAL0RC(String, _generate_name);
GDVIRTUAL0(_setup);
GDVIRTUAL0(_enter);
GDVIRTUAL0(_exit);
GDVIRTUAL1R(Status, _tick, double);
GDVIRTUAL0RC(PackedStringArray, _get_configuration_warnings);
#endif // LIMBOAI_MODULE

public:
// TODO: GDExtension doesn't have this method hmm...
Expand Down
12 changes: 4 additions & 8 deletions hsm/limbo_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,26 @@ LimboState *LimboState::named(const String &p_name) {

void LimboState::_enter() {
active = true;
VCALL(_enter);
GDVIRTUAL_CALL(_enter);
emit_signal(LimboStringNames::get_singleton()->entered);
}

void LimboState::_exit() {
if (!active) {
return;
}
VCALL(_exit);
GDVIRTUAL_CALL(_exit);
emit_signal(LimboStringNames::get_singleton()->exited);
active = false;
}

void LimboState::_update(double p_delta) {
VCALL_ARGS(_update, p_delta);
GDVIRTUAL_CALL(_update, p_delta);
emit_signal(LimboStringNames::get_singleton()->updated, p_delta);
}

void LimboState::_setup() {
VCALL(_setup);
GDVIRTUAL_CALL(_setup);
emit_signal(LimboStringNames::get_singleton()->setup);
}

Expand Down Expand Up @@ -213,14 +213,10 @@ void LimboState::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_blackboard_plan", "plan"), &LimboState::set_blackboard_plan);
ClassDB::bind_method(D_METHOD("get_blackboard_plan"), &LimboState::get_blackboard_plan);

#ifdef LIMBOAI_MODULE
GDVIRTUAL_BIND(_setup);
GDVIRTUAL_BIND(_enter);
GDVIRTUAL_BIND(_exit);
GDVIRTUAL_BIND(_update, "delta");
#elif LIMBOAI_GDEXTENSION
// TODO: Registering virtual functions is not available in godot-cpp...
#endif

ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "EVENT_FINISHED", PROPERTY_HINT_NONE, "", 0), "", "event_finished");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "agent", PROPERTY_HINT_RESOURCE_TYPE, "Node", 0), "set_agent", "get_agent");
Expand Down
3 changes: 1 addition & 2 deletions hsm/limbo_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#endif // LIMBOAI_MODULE

#ifdef LIMBOAI_GDEXTENSION
#include <godot_cpp/core/gdvirtual.gen.inc>
#include <godot_cpp/templates/hash_map.hpp>
#endif // LIMBOAI_GDEXTENSION

Expand Down Expand Up @@ -59,12 +60,10 @@ class LimboState : public Node {
virtual void _exit();
virtual void _update(double p_delta);

#ifdef LIMBOAI_MODULE
GDVIRTUAL0(_setup);
GDVIRTUAL0(_enter);
GDVIRTUAL0(_exit);
GDVIRTUAL1(_update, double);
#endif // LIMBOAI_MODULE

public:
void set_blackboard_plan(const Ref<BlackboardPlan> &p_plan);
Expand Down
71 changes: 0 additions & 71 deletions util/limbo_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,33 +62,6 @@ _FORCE_INLINE_ bool OBJECT_HAS_PROPERTY(Object *p_obj, const StringName &p_prop)

#define VARIANT_EVALUATE(m_op, m_lvalue, m_rvalue, r_ret) r_ret = Variant::evaluate(m_op, m_lvalue, m_rvalue)

// * Virtual calls

#define VCALL(m_name, ...) (GDVIRTUAL_CALL(m_name, __VA_ARGS__))
#define VCALL_ARGS(m_name, ...) (GDVIRTUAL_CALL(m_name, __VA_ARGS__))
#define VCALL_V(m_name, r_ret) (GDVIRTUAL_CALL(m_name, r_ret))
#define VCALL_ARGS_V(m_name, r_ret, ...) (GDVIRTUAL_CALL(m_name, __VA_ARGS__, r_ret))

#define VCALL_OR_NATIVE(m_name) \
if (!GDVIRTUAL_CALL(m_name)) { \
m_name(); \
}

#define VCALL_OR_NATIVE_ARGS(m_name, ...) \
if (!GDVIRTUAL_CALL(m_name, __VA_ARGS__)) { \
m_name(__VA_ARGS__); \
}

#define VCALL_OR_NATIVE_V(m_name, m_ret_type, r_ret) \
if (!GDVIRTUAL_CALL(m_name, r_ret)) { \
r_ret = VariantCaster<m_ret_type>::cast(m_name()); \
}

#define VCALL_OR_NATIVE_ARGS_V(m_name, m_ret_type, r_ret, ...) \
if (!GDVIRTUAL_CALL(m_name, __VA_ARGS__, r_ret)) { \
r_ret = VariantCaster<m_ret_type>::cast(m_name(__VA_ARGS__)); \
}

// * Enum

#define LW_KEY(key) (Key::key)
Expand Down Expand Up @@ -150,50 +123,6 @@ _FORCE_INLINE_ bool OBJECT_HAS_PROPERTY(Object *p_obj, const StringName &p_prop)
Variant::evaluate(m_op, m_lvalue, m_rvalue, r_ret, r_valid); \
}

// * Virtual calls:
// * This is a workaround for missing ClassDB::add_virtual_method().
// ! When using these macros, DON'T BIND the native virtual methods!
// -----------------------------
// VCALL*: only calls a script version if present.
// VCALL_OR_NATIVE*: calls a script version if present; otherwise, calls the native version.

#define VCALL(m_name) \
if (has_method(LW_NAME(m_name))) { \
call(LW_NAME(m_name)); \
}

#define VCALL_ARGS(m_name, ...) \
if (has_method(LW_NAME(m_name))) { \
call(LW_NAME(m_name), __VA_ARGS__); \
}

#define VCALL_V(m_name, r_ret) \
if (has_method(LW_NAME(m_name))) { \
r_ret = call(LW_NAME(m_name)); \
}

#define VCALL_ARGS_V(m_name, r_ret, ...) \
if (has_method(LW_NAME(m_name))) { \
r_ret = call(LW_NAME(m_name, __VA_ARGS__)); \
}

#define VCALL_OR_NATIVE(m_name) \
if (has_method(LW_NAME(m_name))) { \
call(LW_NAME(m_name)); \
} else { \
m_name(); \
}

#define VCALL_OR_NATIVE_ARGS(m_name, ...) \
if (has_method(LW_NAME(m_name))) { \
call(LW_NAME(m_name), __VA_ARGS__); \
} else { \
m_name(__VA_ARGS__); \
}

#define VCALL_OR_NATIVE_V(m_name, m_ret_type, r_ret) r_ret = (has_method(LW_NAME(m_name)) ? VariantCaster<m_ret_type>::cast(call(LW_NAME(m_name))) : VariantCaster<m_ret_type>::cast(m_name()))
#define VCALL_OR_NATIVE_ARGS_V(m_name, m_ret_type, r_ret, ...) r_ret = (has_method(LW_NAME(m_name)) ? VariantCaster<m_ret_type>::cast(call(LW_NAME(m_name), __VA_ARGS__)) : VariantCaster<m_ret_type>::cast(m_name(__VA_ARGS__)))

// * Enum

#define LW_KEY(key) (Key::KEY_##key)
Expand Down

0 comments on commit cd0bc8e

Please sign in to comment.