How do i create holes on NavigationRegion2D?

Godot Version

4.2.1

Question

I want add holes on my NavigationRegion2D, everytime i put one building on the map i want make a hole on the building location so units will avoid the tower on pathfind, i tryd but i have no results

Here i setup the Navigation Area

public virtual void BuildPathfindArea(){
		GD.Print(bounds.ToString());
		
		int cellWidth = Startup.cellSize/2;
		Vector2[] points = new Vector2[4];

		points[0] = new Vector2(bounds.minX,bounds.minY);
		points[1] = new Vector2(bounds.maxX,bounds.minY);
		points[2] = new Vector2(bounds.maxX,bounds.maxY);
		points[3] = new Vector2(bounds.minX,bounds.maxY);

		for (int i = 0; i < points.Length;i++){
			Vector2 point = points[i];
			if (i == 0) {
				points[i] = buildingPlacer.PlacingTilemap.MapToLocal((Vector2I)point) + new Vector2(0,-cellWidth/2);
			}else if (i == 1) {
				//points[i] = buildingPlacer.PlacingTilemap.MapToLocal((Vector2I)point) + new Vector2(-cellWidth,0);
				points[i] = buildingPlacer.PlacingTilemap.MapToLocal((Vector2I)point) + new Vector2(cellWidth,0);
			}else if (i == 2){
				points[i] = buildingPlacer.PlacingTilemap.MapToLocal((Vector2I)point) + new Vector2(0,cellWidth/2);
			}else{
				//points[i] = buildingPlacer.PlacingTilemap.MapToLocal((Vector2I)point) + new Vector2(cellWidth,0);
				points[i] = buildingPlacer.PlacingTilemap.MapToLocal((Vector2I)point) + new Vector2(-cellWidth,0);
			}
		}

		navigationPolygon = pathfindRegion.NavigationPolygon;
		navigationMesh = new NavigationMeshSourceGeometryData2D();

		navigationPolygon.AgentRadius = 0;

		NavigationServer2D.ParseSourceGeometryData(navigationPolygon,navigationMesh,this);

		navigationMesh.AddTraversableOutline(points);

		NavigationServer2D.BakeFromSourceGeometryData(navigationPolygon,navigationMesh);

		Rid rid = pathfindRegion.GetRid();
		NavigationServer2D.RegionSetNavigationPolygon(rid, navigationPolygon);
		NavigationServer2D.RegionSetEnabled(rid, true);
	    NavigationServer2D.RegionSetMap(rid, GetWorld2D().NavigationMap);
	}

Here i try add the hole when one building is placed

public virtual void SetuptObstacle(){
		Polygon2D polygon2D = new Polygon2D();

		float bodyWidth = Startup.cellSize / 4 * bodySize;
		float bodyHeight = bodyWidth / 2;
		List<Vector2> points = new List<Vector2>();

		for (float width = 0f; width < 2*Mathf.Pi; width+=2*Mathf.Pi/bodyWidth){
			Vector2 point = new Vector2(bodyWidth*Mathf.Cos(width),bodyHeight*Mathf.Sin(width));
			 
			points.Add(point);
		}

		polygon2D.Position = Position;
		polygon2D.Polygon = points.ToArray();

		world.PathfindRegion.AddChild(polygon2D);

		NavigationServer2D.BakeFromSourceGeometryData(world.NavigationPolygon,world.NavigationMesh);
    }

Here said i could use Polygons2d Using navigation meshes — Godot Engine (latest) documentation in English that is what i did, but dont work, the Polygon is on the correctly spot / shape but still doenst work, let me show a video:

Testing

Server created regions do not auto update or have any “link” to your NavigationPolygon Resource. If you do things manually in script with the NavigationServer API you also need to do all the updates manually. After baking set the navigation polygon again for the region that you want to update with it.

you mean i need do

NavigationServer2D.RegionSetNavigationPolygon(rid, navigationPolygon);

again?

Well i did it, but doenst work, i also realise that polygons that are already on the navigation map works

but not when i add it via code

I tried to recreate your C# code with GDScript and it works fine doing the exact same steps using a traversable outline for a 100 x 100 pixel navmesh and a placed Polygon2D child node as obstruction.

I can only assume that your input arrays are off or that it is some specific issue with C#, e.g. some wrong type that the C# API can’t convert. That is a very common bug source when the issues only exist in C# code but not GDscript.

I can’t really help you with C# specific issues as I don’t use C# but multiple C# users in the recent past had issues with PackedArrays in Godot that require some specific C# array type or they would turn empty. Apparently that info is somewhere in the C# documentation and they did not see it but I don’t know for sure.