My apologies but I’m afraid I am deeply confused by the Navigation Layers in TileMaps. The Navigation Area of a tilemap is the “solid” color blocks painted on the Navigation Layer, which provides the walkable space, correct? So if I have a “ground” tile on a space, that area will be walkable, and in order to make a non-walkable space, I will need to have a non-walkable tile.
Does this mean that ALL tiles will need to be drawn on Layer 0, removing the ability to layer tilemaps for features such as transparency? Or would I need to create an “invisible” simple tilemap of only blocks and clear spaces to sit on Layer 0, behind my “visual” tilemaps?
I apologize if this is a simple question but I’m finding it extremely difficult to find information on these systems.
Edit: I was able to generate a tilemap by following the following steps:
- Create a NavigationRegion2D
- Add the Tilemap under it
- On Layer 0, add any collision bodies.
- Under the NavigationPolygon, change “SourceGeometryMode” to “Group with Children”
- Add the “Source Geometry Group Name” to the Tilemap as a Group
- Draw a bounding box around the level’s geometry in the NavigationRegion2D (MANDATORY, as otherwise you cannot bake a NavigationPolygon)
- Bake NavigationPolygon
So it seems like the Navigation layers on a tilemap don’t actually matter, and that it’s necessary to put any objects that need to be read by the navigation mesh into a single group. Is this correct?
Edit2:
Moving on from that, I’ve attempted to once again create a mesh from code. This time, I’ve got a NavigationRegion2D in the scene with a NavigationPolygon attached. The NavigationPolygon is set to “Group with Children”, and the TileMap (itself a child of the NavigationRegion2D) is in that group.
My plan would be that this would allow me to generate tiles on the TileMap, and simply regenerate the NavigationRegion2D’s NavigationPolygon as the layout changes.
I used the following code to try and bake the NavigationPolygon, working from the example found here NavigationRegion2D baking with groups (explicit or recursive) doesn't work as expected at runtime (but it does in the editor) · Issue #86414 · godotengine/godot · GitHub in which the user is able to generate NavigationPolygons from code:
public partial class NavRegion_Generator : NavigationRegion2D
{
Callable Call_Parse;
Callable Call_Bake;
NavigationMeshSourceGeometryData2D sourceGeo;
public override void _Ready()
{
Call_Parse = new Callable(this, Navpath.MethodName.OnParseComplete);
Call_Bake = new Callable(this, Navpath.MethodName.OnBakeComplete);
CreateNewNavPolygon();
}
public void CreateNewNavPolygon()
{
this.NavigationPolygon.Clear();
sourceGeo = new NavigationMeshSourceGeometryData2D();
CallDeferred(Navpath.MethodName.ParseSourceGeometry);
}
public void ParseSourceGeometry()
{
NavigationServer2D.ParseSourceGeometryData(this.NavigationPolygon, sourceGeo, this, Call_Parse);
}
public void OnParseComplete()
{
NavigationServer2D.BakeFromSourceGeometryDataAsync(this.NavigationPolygon, sourceGeo, Call_Bake);
}
public void OnBakeComplete()
{
GD.Print("BAKE COMPLETE");
}
}
I can confirm that OnBakeComplete does run, but this generates no NavigationPolygon. I have also rewritten the same code in GDScript and still get no result. The code can be found below:
extends NavigationRegion2D
var Call_Parse : Callable
var Call_Bake : Callable
var sourceGeo : NavigationMeshSourceGeometryData2D
# Called when the node enters the scene tree for the first time.
func _ready():
Call_Parse = Callable(self, "OnParseComplete")
Call_Bake = Callable(self, "OnBakeComplete")
CreateNewNavPolygon()
func CreateNewNavPolygon():
self.navigation_polygon.clear()
sourceGeo = NavigationMeshSourceGeometryData2D.new()
call_deferred("ParseSourceGeometry");
func ParseSourceGeometry():
NavigationServer2D.parse_source_geometry_data(self.navigation_polygon, sourceGeo, self, Call_Parse)
func OnParseComplete():
NavigationServer2D.bake_from_source_geometry_data_async(self.navigation_polygon, sourceGeo, Call_Bake)
func OnBakeComplete():
print("BAKE COMPLETE");
Edit3:
I’ve looked here Using navigation meshes — Godot Engine (latest) documentation in English as well, and tried using the code provided. I’ve pasted the code I’ve used below:
public partial class NavMesh_TestTwo : NavigationRegion2D
{
NavigationPolygon navPolygon;
Rid regionRID;
public override void _Ready()
{
navPolygon = new NavigationPolygon();
regionRID = NavigationServer2D.RegionCreate();
NavigationServer2D.RegionSetEnabled(regionRID, true);
NavigationServer2D.RegionSetMap(regionRID, GetWorld2D().NavigationMap);
navPolygon.Vertices = new Vector2[] {new Vector2(0.0f, 0.0f),
new Vector2(100.0f, 0.0f),
new Vector2(100.0f, 100.0f),
new Vector2(0.0f, 100.0f) };
navPolygon.AddPolygon(new int[] { 0, 1, 2, 3 });
NavigationServer2D.RegionSetNavigationPolygon(regionRID, navPolygon);
}
}
As none of these scripts output a NavigationPolygon, it seems that all my attempts to generate a NagivationPolygon have come to nothing. I’m at a bit of a loss as to where to go from here.