From 721c99a530d0e81ff832001d2eb902a17bdea5b1 Mon Sep 17 00:00:00 2001 From: smix8 <52464204+smix8@users.noreply.github.com> Date: Wed, 22 Jun 2022 10:16:38 +0200 Subject: [PATCH] Add NavigationServer map_force_update() function Adds map_force_update() function to NavigationServer. This function immediately flushes the Navigationserver command queue and recalculates all navigationmeshes and region connections for a specific map. (cherry picked from commit fdea2698053b616d0805ef3c4501a76945ce82ef) --- doc/classes/Navigation2DServer.xml | 10 ++++++++++ doc/classes/NavigationServer.xml | 10 ++++++++++ modules/navigation/godot_navigation_server.cpp | 9 +++++++++ modules/navigation/godot_navigation_server.h | 2 ++ servers/navigation_2d_server.cpp | 5 +++++ servers/navigation_2d_server.h | 2 ++ servers/navigation_server.cpp | 1 + servers/navigation_server.h | 2 ++ 8 files changed, 41 insertions(+) diff --git a/doc/classes/Navigation2DServer.xml b/doc/classes/Navigation2DServer.xml index 4e89e4f9fdc..5b03f494e6e 100644 --- a/doc/classes/Navigation2DServer.xml +++ b/doc/classes/Navigation2DServer.xml @@ -139,6 +139,16 @@ Create a new map. + + + + + This function immediately forces synchronization of the specified navigation [code]map[/code] [RID]. By default navigation maps are only synchronized at the end of each physics frame. This function can be used to immediately (re)calculate all the navigation meshes and region connections of the navigation map. This makes it possible to query a navigation path for a changed map immediately and in the same frame (multiple times if needed). + Due to technical restrictions the current NavigationServer command queue will be flushed. This means all already queued update commands for this physics frame will be executed, even those intended for other maps, regions and agents not part of the specified map. The expensive computation of the navigation meshes and region connections of a map will only be done for the specified map. Other maps will receive the normal synchronization at the end of the physics frame. Should the specified map receive changes after the forced update it will update again as well when the other maps receive their update. + Avoidance processing and dispatch of the [code]safe_velocity[/code] signals is untouched by this function and continues to happen for all maps and agents at the end of the physics frame. + [b]Note:[/b] With great power comes great responsibility. This function should only be used by users that really know what they are doing and have a good reason for it. Forcing an immediate update of a navigation map requires locking the NavigationServer and flushing the entire NavigationServer command queue. Not only can this severely impact the performance of a game but it can also introduce bugs if used inappropriately without much foresight. + + diff --git a/doc/classes/NavigationServer.xml b/doc/classes/NavigationServer.xml index 3e9e1b91350..38a09c0486a 100644 --- a/doc/classes/NavigationServer.xml +++ b/doc/classes/NavigationServer.xml @@ -138,6 +138,16 @@ Create a new map. + + + + + This function immediately forces synchronization of the specified navigation [code]map[/code] [RID]. By default navigation maps are only synchronized at the end of each physics frame. This function can be used to immediately (re)calculate all the navigation meshes and region connections of the navigation map. This makes it possible to query a navigation path for a changed map immediately and in the same frame (multiple times if needed). + Due to technical restrictions the current NavigationServer command queue will be flushed. This means all already queued update commands for this physics frame will be executed, even those intended for other maps, regions and agents not part of the specified map. The expensive computation of the navigation meshes and region connections of a map will only be done for the specified map. Other maps will receive the normal synchronization at the end of the physics frame. Should the specified map receive changes after the forced update it will update again as well when the other maps receive their update. + Avoidance processing and dispatch of the [code]safe_velocity[/code] signals is untouched by this function and continues to happen for all maps and agents at the end of the physics frame. + [b]Note:[/b] With great power comes great responsibility. This function should only be used by users that really know what they are doing and have a good reason for it. Forcing an immediate update of a navigation map requires locking the NavigationServer and flushing the entire NavigationServer command queue. Not only can this severely impact the performance of a game but it can also introduce bugs if used inappropriately without much foresight. + + diff --git a/modules/navigation/godot_navigation_server.cpp b/modules/navigation/godot_navigation_server.cpp index 410e0a164f1..8b90ccfeb44 100644 --- a/modules/navigation/godot_navigation_server.cpp +++ b/modules/navigation/godot_navigation_server.cpp @@ -619,6 +619,15 @@ void GodotNavigationServer::flush_queries() { commands.clear(); } +void GodotNavigationServer::map_force_update(RID p_map) { + NavMap *map = map_owner.getornull(p_map); + ERR_FAIL_COND(map == nullptr); + + flush_queries(); + + map->sync(); +} + void GodotNavigationServer::process(real_t p_delta_time) { flush_queries(); diff --git a/modules/navigation/godot_navigation_server.h b/modules/navigation/godot_navigation_server.h index 537e79d033e..faf76817a90 100644 --- a/modules/navigation/godot_navigation_server.h +++ b/modules/navigation/godot_navigation_server.h @@ -111,6 +111,8 @@ public: virtual Array map_get_regions(RID p_map) const; virtual Array map_get_agents(RID p_map) const; + virtual void map_force_update(RID p_map); + virtual RID region_create() const; COMMAND_2(region_set_enter_cost, RID, p_region, real_t, p_enter_cost); diff --git a/servers/navigation_2d_server.cpp b/servers/navigation_2d_server.cpp index f141aaaddc7..b35814f1959 100644 --- a/servers/navigation_2d_server.cpp +++ b/servers/navigation_2d_server.cpp @@ -188,6 +188,7 @@ void Navigation2DServer::_bind_methods() { ClassDB::bind_method(D_METHOD("map_get_regions", "map"), &Navigation2DServer::map_get_regions); ClassDB::bind_method(D_METHOD("map_get_agents", "map"), &Navigation2DServer::map_get_agents); + ClassDB::bind_method(D_METHOD("map_force_update", "map"), &Navigation2DServer::map_force_update); ClassDB::bind_method(D_METHOD("region_create"), &Navigation2DServer::region_create); ClassDB::bind_method(D_METHOD("region_set_enter_cost", "region", "enter_cost"), &Navigation2DServer::region_set_enter_cost); @@ -251,6 +252,10 @@ void FORWARD_2_C(map_set_active, RID, p_map, bool, p_active, rid_to_rid, bool_to bool FORWARD_1_C(map_is_active, RID, p_map, rid_to_rid); +void Navigation2DServer::map_force_update(RID p_map) { + NavigationServer::get_singleton_mut()->map_force_update(p_map); +} + void FORWARD_2_C(map_set_cell_size, RID, p_map, real_t, p_cell_size, rid_to_rid, real_to_real); real_t FORWARD_1_C(map_get_cell_size, RID, p_map, rid_to_rid); diff --git a/servers/navigation_2d_server.h b/servers/navigation_2d_server.h index 64e6fbdd2ee..41d7786be5d 100644 --- a/servers/navigation_2d_server.h +++ b/servers/navigation_2d_server.h @@ -89,6 +89,8 @@ public: virtual Array map_get_regions(RID p_map) const; virtual Array map_get_agents(RID p_map) const; + virtual void map_force_update(RID p_map); + /// Creates a new region. virtual RID region_create() const; diff --git a/servers/navigation_server.cpp b/servers/navigation_server.cpp index db7ef102a5c..582fcb536b3 100644 --- a/servers/navigation_server.cpp +++ b/servers/navigation_server.cpp @@ -54,6 +54,7 @@ void NavigationServer::_bind_methods() { ClassDB::bind_method(D_METHOD("map_get_regions", "map"), &NavigationServer::map_get_regions); ClassDB::bind_method(D_METHOD("map_get_agents", "map"), &NavigationServer::map_get_agents); + ClassDB::bind_method(D_METHOD("map_force_update", "map"), &NavigationServer::map_force_update); ClassDB::bind_method(D_METHOD("region_create"), &NavigationServer::region_create); ClassDB::bind_method(D_METHOD("region_set_enter_cost", "region", "enter_cost"), &NavigationServer::region_set_enter_cost); diff --git a/servers/navigation_server.h b/servers/navigation_server.h index 68aa262fb39..5e934352e80 100644 --- a/servers/navigation_server.h +++ b/servers/navigation_server.h @@ -102,6 +102,8 @@ public: virtual Array map_get_regions(RID p_map) const = 0; virtual Array map_get_agents(RID p_map) const = 0; + virtual void map_force_update(RID p_map) = 0; + /// Creates a new region. virtual RID region_create() const = 0;