From cd24a63da2efd8f5b6bdc0072e81c35e2d1986fe Mon Sep 17 00:00:00 2001 From: smix8 <52464204+smix8@users.noreply.github.com> Date: Fri, 16 Apr 2021 22:04:02 +0200 Subject: [PATCH 1/2] Fix NavigationAgent2D not emitting "target_reached" Signal reliably Fix NavigationAgent2D not emitting "target_reached" Signal reliably --- scene/2d/navigation_agent_2d.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/scene/2d/navigation_agent_2d.cpp b/scene/2d/navigation_agent_2d.cpp index a18687afed7..0d23afe77f5 100644 --- a/scene/2d/navigation_agent_2d.cpp +++ b/scene/2d/navigation_agent_2d.cpp @@ -301,6 +301,12 @@ void NavigationAgent2D::update_navigation() { while (o.distance_to(navigation_path[nav_path_index]) < target_desired_distance) { nav_path_index += 1; if (nav_path_index == navigation_path.size()) { + if (!target_reached) { + if (distance_to_target() < target_desired_distance) { + emit_signal("target_reached"); + target_reached = true; + } + } nav_path_index -= 1; navigation_finished = true; emit_signal("navigation_finished"); From 0b87cb5e3d75a8967fe85e5fc4afe150b3c810a2 Mon Sep 17 00:00:00 2001 From: smix8 <52464204+smix8@users.noreply.github.com> Date: Tue, 27 Apr 2021 11:22:37 +0200 Subject: [PATCH 2/2] put distance check to target into function put distance check to target into function --- scene/2d/navigation_agent_2d.cpp | 23 +++++++++++------------ scene/2d/navigation_agent_2d.h | 1 + 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/scene/2d/navigation_agent_2d.cpp b/scene/2d/navigation_agent_2d.cpp index 0d23afe77f5..5ecf897ebdf 100644 --- a/scene/2d/navigation_agent_2d.cpp +++ b/scene/2d/navigation_agent_2d.cpp @@ -100,12 +100,7 @@ void NavigationAgent2D::_notification(int p_what) { case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: { if (agent_parent) { NavigationServer2D::get_singleton()->agent_set_position(agent, agent_parent->get_global_transform().get_origin()); - if (!target_reached) { - if (distance_to_target() < target_desired_distance) { - emit_signal("target_reached"); - target_reached = true; - } - } + _check_distance_to_target(); } } break; } @@ -301,12 +296,7 @@ void NavigationAgent2D::update_navigation() { while (o.distance_to(navigation_path[nav_path_index]) < target_desired_distance) { nav_path_index += 1; if (nav_path_index == navigation_path.size()) { - if (!target_reached) { - if (distance_to_target() < target_desired_distance) { - emit_signal("target_reached"); - target_reached = true; - } - } + _check_distance_to_target(); nav_path_index -= 1; navigation_finished = true; emit_signal("navigation_finished"); @@ -315,3 +305,12 @@ void NavigationAgent2D::update_navigation() { } } } + +void NavigationAgent2D::_check_distance_to_target() { + if (!target_reached) { + if (distance_to_target() < target_desired_distance) { + emit_signal("target_reached"); + target_reached = true; + } + } +} diff --git a/scene/2d/navigation_agent_2d.h b/scene/2d/navigation_agent_2d.h index 138ba3bc642..234cad333fd 100644 --- a/scene/2d/navigation_agent_2d.h +++ b/scene/2d/navigation_agent_2d.h @@ -140,6 +140,7 @@ public: private: void update_navigation(); + void _check_distance_to_target(); }; #endif