How do I get AStarGrid2D from TileMap?

Godot Version

4.2

Question

I have am using navigation, with the grid being created by a navigation layer on my tilemap.

I want to change settings on it like DiagonalMode and heuristic to try to make the paths better, but I can’t for the life of me figure out how to get the references to the AStarGrid2D.

I can do this:

	var tilemap = get_node("/root/Level/TileMap")
	var astargrid_rid = tilemap.get_navigation_map(0)

but it gives me an RID, not a reference to the tilemap.

It seems you’re supposed to use the NavigationServer2D, but I can’t find the right method to call to get the AStarGrid2D using the RID, plus at the top it says “NavigationServer2D is the server that handles navigation maps, regions and agents. It does not handle A* navigation from AStar2D or AStarGrid2D.” which makes it seem like im in the wrong place.

How do I get AStarGrid2D from TileMap?

AStarGrid2D and the NavigationServer2D are different things. You can’t get an AStarGrid2D from a TileMap directly. You’ll need to build your own AStarGrid2D if you want to use one.

So I can’t tweak any of the settings on the grid? I’m having trouble with the tile based one because it doesn’t seem to use diagonals, my paths seem very inefficient and cause my characters to get stuck. Was hoping there would be a way to tweak it.

Would also be nice if i could make those tiles still navigable, but deprioritized, so they avoid them there’s a better route nearby

AStarGrid2D is its own helper class completely detached from everything else, especially from the rest of the navigation system. That said for the kind of pathfinding you are after with that TileMap layout it might be the best option but you need to populate it manually in script.

The NavigationServer and navigation mesh are really not designed to work with small grid cell movement. You would need to bake your TileMap with a NavigationRegion2D to avoid getting stuck on all the tile cell corners. You would still have no rigid movement constrains like “diagonal” or other common constrains that only make sense for cell-based pathfinding but not surface-based like navigation mesh.

2 Likes