#Trying to remove navigation on 2d tilemap at runtime has unexpected results

1 messages · Page 1 of 1 (latest)

drowsy spear
#
        foreach (var cell in cells)
        {
            if (GD.Randf() < 0.03 && cell != PlayerSpawnCoords)
            {
                Node2D appleTree = ReusableStatics.AppleTree.Instantiate<Node2D>();
                appleTree.Position = MapToLocal(cell);
                AddChild(appleTree);
                GetCellTileData(0, cell).SetNavigationPolygon(0, null);
            }
        }

I'm trying to dynamically remove navigation from specific cells, but I am getting extremely unexpected results(Screenshot), all tiles should have navigation except the cells with trees on them, but as you can see it looks like it's just.. random?

#

That is the only place I dynamically set the polygon, I added a print with cell coords to be sure it wasn't somehow running in places it shouldn't be and I got correct coords 🤔

#

It looks to me like it's applying the polygon to the actual tile instead of the cell so I guess that makes sense, is it not possible to remove navigation on specific tiles?

#

Okay found some information, in case anyone else finds this in the future the solution was this:

    private List<Vector2I> RemoveNavigation = new List<Vector2I>();

    public override bool _UseTileDataRuntimeUpdate(int layer, Vector2I coords)
    {
        if (RemoveNavigation.Contains(coords))
            return true;
        return base._UseTileDataRuntimeUpdate(layer, coords);
    }

    public override void _TileDataRuntimeUpdate(int layer, Vector2I coords, TileData tileData)
    {
        tileData.SetNavigationPolygon(0, null);
    }

Add any tile coords to RemoveNavigation you want removed.

rocky oriole
#

I dont think this is scalable for changes runtime. The Tilemap will give you major performance issues if you change the cell data like this, on top of the navigation map needing to redo all those TileMap navigation regions that get send again.

#

For navmesh runtime changes you usually want to stay away from the TileMap build-in navigation and use a chunk system.