diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2023-01-28 15:58:49 +0100 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2023-01-28 15:58:49 +0100 |
commit | 3a25148b9bf1ab07549a18cc6f7691052d576c69 (patch) | |
tree | f9f06371e056bcc831092464d3bceedaced79ef2 /modules | |
parent | e5752fdfd33a35be5a96fe754c5553fee88cbe76 (diff) | |
parent | 22f9ef19e1accd5d5e36ce30589ba3edd5c3d0d0 (diff) |
Merge pull request #72228 from DarkKilauea/nav-agent-callable
Use Callable for Navigation Agent callbacks
Diffstat (limited to 'modules')
-rw-r--r-- | modules/navigation/godot_navigation_server.cpp | 41 | ||||
-rw-r--r-- | modules/navigation/godot_navigation_server.h | 7 | ||||
-rw-r--r-- | modules/navigation/rvo_agent.cpp | 32 | ||||
-rw-r--r-- | modules/navigation/rvo_agent.h | 13 |
4 files changed, 19 insertions, 74 deletions
diff --git a/modules/navigation/godot_navigation_server.cpp b/modules/navigation/godot_navigation_server.cpp index 5baf6db2e8..d546c5d3ba 100644 --- a/modules/navigation/godot_navigation_server.cpp +++ b/modules/navigation/godot_navigation_server.cpp @@ -81,36 +81,6 @@ using namespace NavigationUtilities; } \ void GodotNavigationServer::MERGE(_cmd_, F_NAME)(T_0 D_0, T_1 D_1) -#define COMMAND_4(F_NAME, T_0, D_0, T_1, D_1, T_2, D_2, T_3, D_3) \ - struct MERGE(F_NAME, _command) : public SetCommand { \ - T_0 d_0; \ - T_1 d_1; \ - T_2 d_2; \ - T_3 d_3; \ - MERGE(F_NAME, _command) \ - ( \ - T_0 p_d_0, \ - T_1 p_d_1, \ - T_2 p_d_2, \ - T_3 p_d_3) : \ - d_0(p_d_0), \ - d_1(p_d_1), \ - d_2(p_d_2), \ - d_3(p_d_3) {} \ - virtual void exec(GodotNavigationServer *server) override { \ - server->MERGE(_cmd_, F_NAME)(d_0, d_1, d_2, d_3); \ - } \ - }; \ - void GodotNavigationServer::F_NAME(T_0 D_0, T_1 D_1, T_2 D_2, T_3 D_3) { \ - auto cmd = memnew(MERGE(F_NAME, _command)( \ - D_0, \ - D_1, \ - D_2, \ - D_3)); \ - add_command(cmd); \ - } \ - void GodotNavigationServer::MERGE(_cmd_, F_NAME)(T_0 D_0, T_1 D_1, T_2 D_2, T_3 D_3) - GodotNavigationServer::GodotNavigationServer() {} GodotNavigationServer::~GodotNavigationServer() { @@ -711,17 +681,17 @@ bool GodotNavigationServer::agent_is_map_changed(RID p_agent) const { return agent->is_map_changed(); } -COMMAND_4(agent_set_callback, RID, p_agent, ObjectID, p_object_id, StringName, p_method, Variant, p_udata) { +COMMAND_2(agent_set_callback, RID, p_agent, Callable, p_callback) { RvoAgent *agent = agent_owner.get_or_null(p_agent); ERR_FAIL_COND(agent == nullptr); - agent->set_callback(p_object_id, p_method, p_udata); + agent->set_callback(p_callback); if (agent->get_map()) { - if (p_object_id == ObjectID()) { - agent->get_map()->remove_agent_as_controlled(agent); - } else { + if (p_callback.is_valid()) { agent->get_map()->set_agent_as_controlled(agent); + } else { + agent->get_map()->remove_agent_as_controlled(agent); } } } @@ -946,4 +916,3 @@ int GodotNavigationServer::get_process_info(ProcessInfo p_info) const { #undef COMMAND_1 #undef COMMAND_2 -#undef COMMAND_4 diff --git a/modules/navigation/godot_navigation_server.h b/modules/navigation/godot_navigation_server.h index efefa60de8..eea5713c40 100644 --- a/modules/navigation/godot_navigation_server.h +++ b/modules/navigation/godot_navigation_server.h @@ -54,10 +54,6 @@ virtual void F_NAME(T_0 D_0, T_1 D_1) override; \ void MERGE(_cmd_, F_NAME)(T_0 D_0, T_1 D_1) -#define COMMAND_4_DEF(F_NAME, T_0, D_0, T_1, D_1, T_2, D_2, T_3, D_3, D_3_DEF) \ - virtual void F_NAME(T_0 D_0, T_1 D_1, T_2 D_2, T_3 D_3 = D_3_DEF) override; \ - void MERGE(_cmd_, F_NAME)(T_0 D_0, T_1 D_1, T_2 D_2, T_3 D_3) - class GodotNavigationServer; struct SetCommand { @@ -182,7 +178,7 @@ public: COMMAND_2(agent_set_position, RID, p_agent, Vector3, p_position); COMMAND_2(agent_set_ignore_y, RID, p_agent, bool, p_ignore); virtual bool agent_is_map_changed(RID p_agent) const override; - COMMAND_4_DEF(agent_set_callback, RID, p_agent, ObjectID, p_object_id, StringName, p_method, Variant, p_udata, Variant()); + COMMAND_2(agent_set_callback, RID, p_agent, Callable, p_callback); COMMAND_1(free, RID, p_object); @@ -198,6 +194,5 @@ public: #undef COMMAND_1 #undef COMMAND_2 -#undef COMMAND_4_DEF #endif // GODOT_NAVIGATION_SERVER_H diff --git a/modules/navigation/rvo_agent.cpp b/modules/navigation/rvo_agent.cpp index 979ef0d917..40f1e925be 100644 --- a/modules/navigation/rvo_agent.cpp +++ b/modules/navigation/rvo_agent.cpp @@ -32,10 +32,6 @@ #include "nav_map.h" -RvoAgent::RvoAgent() { - callback.id = ObjectID(); -} - void RvoAgent::set_map(NavMap *p_map) { map = p_map; } @@ -50,31 +46,25 @@ bool RvoAgent::is_map_changed() { } } -void RvoAgent::set_callback(ObjectID p_id, const StringName p_method, const Variant p_udata) { - callback.id = p_id; - callback.method = p_method; - callback.udata = p_udata; +void RvoAgent::set_callback(Callable p_callback) { + callback = p_callback; } bool RvoAgent::has_callback() const { - return callback.id.is_valid(); + return callback.is_valid(); } void RvoAgent::dispatch_callback() { - if (callback.id.is_null()) { + if (!callback.is_valid()) { return; } - Object *obj = ObjectDB::get_instance(callback.id); - if (!obj) { - callback.id = ObjectID(); - return; - } - - Callable::CallError responseCallError; - callback.new_velocity = Vector3(agent.newVelocity_.x(), agent.newVelocity_.y(), agent.newVelocity_.z()); + Vector3 new_velocity = Vector3(agent.newVelocity_.x(), agent.newVelocity_.y(), agent.newVelocity_.z()); - const Variant *vp[2] = { &callback.new_velocity, &callback.udata }; - int argc = (callback.udata.get_type() == Variant::NIL) ? 1 : 2; - obj->callp(callback.method, vp, argc, responseCallError); + // Invoke the callback with the new velocity. + Variant args[] = { new_velocity }; + const Variant *args_p[] = { &args[0] }; + Variant return_value; + Callable::CallError call_error; + callback.callp(args_p, 1, return_value, call_error); } diff --git a/modules/navigation/rvo_agent.h b/modules/navigation/rvo_agent.h index 7b19907b2b..5f377b6079 100644 --- a/modules/navigation/rvo_agent.h +++ b/modules/navigation/rvo_agent.h @@ -39,21 +39,12 @@ class NavMap; class RvoAgent : public NavRid { - struct AvoidanceComputedCallback { - ObjectID id; - StringName method; - Variant udata; - Variant new_velocity; - }; - NavMap *map = nullptr; RVO::Agent agent; - AvoidanceComputedCallback callback; + Callable callback = Callable(); uint32_t map_update_id = 0; public: - RvoAgent(); - void set_map(NavMap *p_map); NavMap *get_map() { return map; @@ -65,7 +56,7 @@ public: bool is_map_changed(); - void set_callback(ObjectID p_id, const StringName p_method, const Variant p_udata = Variant()); + void set_callback(Callable p_callback); bool has_callback() const; void dispatch_callback(); |