NavigationAgent2D - Where is the (TileMap) data source?

Godot Version

4.3 beta 2

Question

Create several TileMaps. Paint certain tiles navigable. NavigationAgent2D can now find paths. i never tell NavigationAgent2D which TileMap to look at (or TileMapLayer in 4.3). So how does it know?

Motivation: Like most people, i’m trying to get NavigationAgent2D to stop slamming people into walls, a feature Godot does not appear to support. In other threads (if i understood correctly) smix8 mentioned having 2 types of TileMaps - one for graphics, one for navigation. You create the latter by squishing the former together. Something like this:

func _ready():
1    for every tileMap:
2        for every tile:
3            if tile is walkable:
4                combined_data [x,y] = walkable
5            if tile has a physics mask:
6                combined_data [x,y] = not walkable
7    var new_nav_mesh = bake(combined_data)
8    navigationAgent2D.nav_mesh = new_nav_mesh

i can probably figure out lines 1-6 but i have no idea how to do line 8.

Note: i don’t use any class other than NavigationAgent2D. i know there are classes for regions and servers and stuff but no tutorial i’ve used (maybe 10 at this point) uses them and NavigationAgent2D seems to “work” without me manually creating/using them.

If you use a TileMap build-in navigation the TileMap will create a navigation region for every single cell that uses a NavigationPolygon. Each TileMapLayer adds the regions from a layer to a dedicated navigation map. Only the first TileMapLayer adds everything to the default navigation map of the World2D and agents by default also use this navigation map.

Since Godot 4.2 you an use a NavigationRegion2D to bake your TileMap to a more optimizes navigation mesh. This is the far better option because a TileMap can not create navigation meshes with the required offsets towards physics shapes for an agent radius. A TileMap navigation mesh will get your agents stuck on physic shapes like walls and corners.

i might be wrong but if i use a NavigationRegion2D then i can’t (easily or quickly) modify it at runtime, correct? i looked at your PR that sat in waiting-for-review hell for years and it looks like they never approved an easy approach to this problem.

You can change your TileMap cells and rebake but there is no easy function API on the entire TileMap to get just a specific set of cells, so yes, the TileMap is not at all runtime update friendly at its current state.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.