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 ?