Skip to content

Commit

Permalink
Make Navigation Agents and Obstacles respect parent process mode
Browse files Browse the repository at this point in the history
Temporarily removes agent from navigation map when parent node cannot process due to SceneTree pause and process_mode property. Normal process_mode does not work as other agents would still avoid the paused agents because they were still active on the navigation map and the rvo world. Also fixes potential crash when region_get_map or agent_get_map is called while no map is set.

(cherry picked from commit 6b51ab6)
  • Loading branch information
smix8 authored and akien-mga committed May 19, 2022
1 parent 4491569 commit 88acb5b
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 3 deletions.
10 changes: 8 additions & 2 deletions modules/navigation/godot_navigation_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,19 @@ Array GodotNavigationServer::map_get_agents(RID p_map) const {
RID GodotNavigationServer::region_get_map(RID p_region) const {
NavRegion *region = region_owner.getornull(p_region);
ERR_FAIL_COND_V(region == nullptr, RID());
return region->get_map()->get_self();
if (region->get_map()) {
return region->get_map()->get_self();
}
return RID();
}

RID GodotNavigationServer::agent_get_map(RID p_agent) const {
RvoAgent *agent = agent_owner.getornull(p_agent);
ERR_FAIL_COND_V(agent == nullptr, RID());
return agent->get_map()->get_self();
if (agent->get_map()) {
return agent->get_map()->get_self();
}
return RID();
}

RID GodotNavigationServer::region_create() const {
Expand Down
18 changes: 18 additions & 0 deletions scene/2d/navigation_agent_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,24 @@ void NavigationAgent2D::_notification(int p_what) {

set_physics_process_internal(true);
} break;
case NOTIFICATION_PAUSED: {
if (agent_parent && !agent_parent->can_process()) {
map_before_pause = Navigation2DServer::get_singleton()->agent_get_map(get_rid());
Navigation2DServer::get_singleton()->agent_set_map(get_rid(), RID());
} else if (agent_parent && agent_parent->can_process() && !(map_before_pause == RID())) {
Navigation2DServer::get_singleton()->agent_set_map(get_rid(), map_before_pause);
map_before_pause = RID();
}
} break;
case NOTIFICATION_UNPAUSED: {
if (agent_parent && !agent_parent->can_process()) {
map_before_pause = Navigation2DServer::get_singleton()->agent_get_map(get_rid());
Navigation2DServer::get_singleton()->agent_set_map(get_rid(), RID());
} else if (agent_parent && agent_parent->can_process() && !(map_before_pause == RID())) {
Navigation2DServer::get_singleton()->agent_set_map(get_rid(), map_before_pause);
map_before_pause = RID();
}
} break;
case NOTIFICATION_EXIT_TREE: {
agent_parent = nullptr;
set_navigation(nullptr);
Expand Down
1 change: 1 addition & 0 deletions scene/2d/navigation_agent_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class NavigationAgent2D : public Node {
Navigation2D *navigation;

RID agent;
RID map_before_pause;

real_t target_desired_distance;
real_t radius;
Expand Down
18 changes: 18 additions & 0 deletions scene/2d/navigation_obstacle_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,24 @@ void NavigationObstacle2D::_notification(int p_what) {
case NOTIFICATION_UNPARENTED: {
parent_node2d = nullptr;
} break;
case NOTIFICATION_PAUSED: {
if (parent_node2d && !parent_node2d->can_process()) {
map_before_pause = Navigation2DServer::get_singleton()->agent_get_map(get_rid());
Navigation2DServer::get_singleton()->agent_set_map(get_rid(), RID());
} else if (parent_node2d && parent_node2d->can_process() && !(map_before_pause == RID())) {
Navigation2DServer::get_singleton()->agent_set_map(get_rid(), map_before_pause);
map_before_pause = RID();
}
} break;
case NOTIFICATION_UNPAUSED: {
if (parent_node2d && !parent_node2d->can_process()) {
map_before_pause = Navigation2DServer::get_singleton()->agent_get_map(get_rid());
Navigation2DServer::get_singleton()->agent_set_map(get_rid(), RID());
} else if (parent_node2d && parent_node2d->can_process() && !(map_before_pause == RID())) {
Navigation2DServer::get_singleton()->agent_set_map(get_rid(), map_before_pause);
map_before_pause = RID();
}
} break;
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
if (parent_node2d && parent_node2d->is_inside_tree()) {
Navigation2DServer::get_singleton()->agent_set_position(agent, parent_node2d->get_global_transform().get_origin());
Expand Down
3 changes: 2 additions & 1 deletion scene/2d/navigation_obstacle_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ class NavigationObstacle2D : public Node {

Navigation2D *navigation;
Node2D *parent_node2d = nullptr;

RID agent;
RID map_before_pause;

bool estimate_radius = true;
real_t radius = 1.0;

Expand Down
18 changes: 18 additions & 0 deletions scene/3d/navigation_agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,24 @@ void NavigationAgent::_notification(int p_what) {
// the navigation map may not be ready at that time. This fixes issues with taking the agent out of the scene tree.
request_ready();
} break;
case NOTIFICATION_PAUSED: {
if (agent_parent && !agent_parent->can_process()) {
map_before_pause = NavigationServer::get_singleton()->agent_get_map(get_rid());
NavigationServer::get_singleton()->agent_set_map(get_rid(), RID());
} else if (agent_parent && agent_parent->can_process() && !(map_before_pause == RID())) {
NavigationServer::get_singleton()->agent_set_map(get_rid(), map_before_pause);
map_before_pause = RID();
}
} break;
case NOTIFICATION_UNPAUSED: {
if (agent_parent && !agent_parent->can_process()) {
map_before_pause = NavigationServer::get_singleton()->agent_get_map(get_rid());
NavigationServer::get_singleton()->agent_set_map(get_rid(), RID());
} else if (agent_parent && agent_parent->can_process() && !(map_before_pause == RID())) {
NavigationServer::get_singleton()->agent_set_map(get_rid(), map_before_pause);
map_before_pause = RID();
}
} break;
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
if (agent_parent) {
NavigationServer::get_singleton()->agent_set_position(agent, agent_parent->get_global_transform().origin);
Expand Down
1 change: 1 addition & 0 deletions scene/3d/navigation_agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class NavigationAgent : public Node {
Navigation *navigation;

RID agent;
RID map_before_pause;

real_t target_desired_distance;
real_t radius;
Expand Down
18 changes: 18 additions & 0 deletions scene/3d/navigation_obstacle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ void NavigationObstacle::_notification(int p_what) {

set_physics_process_internal(true);
} break;
case NOTIFICATION_PAUSED: {
if (parent_spatial && !parent_spatial->can_process()) {
map_before_pause = NavigationServer::get_singleton()->agent_get_map(get_rid());
NavigationServer::get_singleton()->agent_set_map(get_rid(), RID());
} else if (parent_spatial && parent_spatial->can_process() && !(map_before_pause == RID())) {
NavigationServer::get_singleton()->agent_set_map(get_rid(), map_before_pause);
map_before_pause = RID();
}
} break;
case NOTIFICATION_UNPAUSED: {
if (parent_spatial && !parent_spatial->can_process()) {
map_before_pause = NavigationServer::get_singleton()->agent_get_map(get_rid());
NavigationServer::get_singleton()->agent_set_map(get_rid(), RID());
} else if (parent_spatial && parent_spatial->can_process() && !(map_before_pause == RID())) {
NavigationServer::get_singleton()->agent_set_map(get_rid(), map_before_pause);
map_before_pause = RID();
}
} break;
case NOTIFICATION_EXIT_TREE: {
set_navigation(nullptr);
set_physics_process_internal(false);
Expand Down
2 changes: 2 additions & 0 deletions scene/3d/navigation_obstacle.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class NavigationObstacle : public Node {
Navigation *navigation;

RID agent;
RID map_before_pause;

bool estimate_radius = true;
real_t radius = 1.0;

Expand Down

0 comments on commit 88acb5b

Please sign in to comment.