Bake_navigation_polygon() affects polygons on all navigation regions instead of just one

Godot Version

4.2.2

Question

I have a somewhat procedural level generation that uses nodes that are saved as PackedScene.

I then put rock obstacles as child nodes inside NavigationRegion2D and bake the polygons:

I instantiate them in a level generator like this:

const TILES_CENTER = "res://Scenes/Levels/Modules/CENTER"
var tile :Node2D = _load_tile(tiles_center[randi() % tiles_center.size()])
...
island_tiles.append(tile)
tile.position.x = pos_x
tile.position.y = pos_y
self.add_child(tile)
....
func _load_tile(module_instance_path):
	var module_instance = load(module_instance_path).instantiate()
	if module_instance:
		return module_instance
	else:
		return null

When one of these rocks are destroyed:

func handle_hit(bullet_damage):
	stat_health.health -= bullet_damage
	if stat_health.health <= 0:
		var parent_node = self.get_parent()
		GlobalSignals.emit_signal("obstacle_destroyed", self.global_position)
		parent_node.queue_free()

the NavigationRegion2D which the rock belongs to should be re-baked:

func rebake_navigation_region(position):
	print('should rebake at position x:', position.x, ' y:', position.y)
	var closest_region: NavigationRegion2D = null
	var closest_distance: float = INF
	
	for region in nav_regions:
		var distance = region.global_position.distance_to(position)
		if distance < closest_distance:
			closest_distance = distance
			closest_region = region
	if is_instance_valid(closest_region):
		await get_tree().process_frame
		closest_region.bake_navigation_polygon(true)

what happens here is that once one rock is destroyed, the region is re-baked and the navigation polygon that circle the rock is removed. That’s OK. However, the neighbouring tile then also has its navigation polygon removed, but the rock stays, as if there is some kind of reference of navigation polygon inside the tile. But I don’t understand how that would be possible since every tile is instantiated via _load_tile() method?

Or is the problem that I baked the polygons inside the “Center_Level_Module_01” (ignore parent node name, forgot to rename it) via editor on the PackedScene with the “Bake NavigationPolygon” button ?

Not sure what setup you really did here but you either use the NavigationRegion2D navigation mesh or you use the TileMap build-in navigation. You cant use both at the same time without conflict because their navigation meshes will overlap breaking the pathfinding logic.

So if you use the NavigationRegion2D disable the TileMap TileMapNavigationLayer.

If your TileMap has tiles with navigation mesh you also shouldn’t draw an outline with the NavigationRegion2D over the TileMap as that will cause bugs with the outline solution. It might read some of your tiles as holes and vice versa due to wrong outline order.

1 Like

I did as per your suggestion and disabled the navigation layer on TileMap because I intend to only use NavigationRegion2D.

However, what actually helped me was setting the “Local to Scene” checkbox!

What this local to scene does is it auto-duplicates the NavigationPolygon resource for every single node that uses it creating a full new copy.

Looking at that image the NavigationPolgon resource was created and stored inside the scene file. So before every single scene instance used the same resource affecting each other on any change to the resource data.