NavigationRegion enable/disable dynamically

Godot Version

4.3

Problem

I have a 2D Tower Defense Game with a TileMapLayer currently.
I am adding NavigationRegion2Ds for each cell.

My units are moved by the NavigationAgent example script:

The Towers are StaticBodys. There are some pre-placed on the map.
I iterate through all the towers, get the NavigationRegion2D at that cell position and set its enabled property to false.

The documentation for NavigationRegion2D just says "Existing paths will not be automatically updated when a region gets enabled / disabled."

But that’s exactly what I want in this case. Placeing towers and updating the enabled regions dynamically.

Question

How do I update the computed paths when enabling/disabling individual NavigationRegion2Ds?

The short answer is that you don’t. There’s a longer answer involving calling the server and i’ll let smarter people copy/paste that answer here.

Do you need to use NavigationRegion? i tried for a while and loathed it. i ended up using AStar2DGrid instead and it works pretty well. My TowerDefense game uses it for placing towers, terrain penalties, drawing grid-like paths, etc. If you are willing to consider that, i wrote an article on it:

The article links to a Github repository with lots of different sample projects for 2D pathfinding. i think that one also has a tower defense mazing game in it where you can add and remove walls. i also have a demo on Itch showing how various path finding stuff works (Godot Pathfinding Demo by baylorw). That demo is the code in Github.

The article explains a little of the how/why stuff. If you’re lazy, my Github also has a working tower defense game you can steal the pathfinding code from.

1 Like

Thanks for the article!
I’m using AStarGrid2D currently, but wanted to explore the other options as well.
Mainly because I wanted cleaner diagonal lines to follow and figured I had to use the Navigation* classes for that.

After reading your article, I noticed that I just ignored the jumping_enabled property. Probably because I didn’t really understand what it meant, so thanks for explaining!
I tried it out and it produces the diagonal movement that I wanted. :smiley:
(Although it seems to produce some really “jumping around” paths in some specific edge cases that I just happened to have in between when looking for a closest path to the target because of obstructions)

I figured that I’d have to look at the NavigationServer more closely, but was hoping that I don’t. For the sake of exploring further I guess that’s what I’ll have to do then :smiley:

Thanks again!