How to create gates that only allow some agents

Godot Version

4.4dev7, 4.3

Question

In my top down 2d game I have two groups of agents: player minions and enemies. Both groups are using navigations. World map contains gates. A gate follows this set of rules:

  1. Gate can belong to player (minions) or enemy
  2. Agents that match gate can pass freely like it doesn’t exist.
  3. Gate can be destroyed. When destroyed everyone can pass freely.
  4. Destroyed gate can be rebuild. Rebuilding gate works as new

Here is example situation with friendly gate. Minions that need to go to some point beyond gate will have path calculate like there is navigation region. Enemies navigation that targets things on the other side will be calculated like there is end of navigation. Once enemies get closer to gate they will attack it. Once gate is destroyed navigation for enemies will work the same way as for minions.

Here is my current solution.

I separated world map into smaller regions. On image you can see “common” regions on left and right and then “gate” region in center. “Gate” region only have layers turned on for players, so enemies can’t go. When game starts I have a script that makes a copy of “gate” region but sets layers that only allow enemies to pass. New region is disabled by default. When gate is destroyed this region is activated. When gate is rebuilt then region is deactivated.

Solution itself works fine. However, development experience is very suboptimal. I have to create many regions manually and do a lot of pixel hunting to ensure edges are aligned. It means every time I move anything, I have to do it again. Process is very error prone and I have to test in game with real agents.

My main question: is there a better way to avoid manual error prone labor?. Ideally, something that works out of the box, even if it’s 3rd party navigation plugin.
Another solution I was thinking of is to chunk regions myself from code, but don’t know if it’s possible and if it’s a good idea.

Not sure why you are adding or removing a duplicated “gate” region for you enemies. You could just switch on a navigation layer bit when the “gate is destroyed” and you enemies can use the region and disable it again if rebuild, no? Having two regions overlap will give you navigation map edge merge errors.

In general that is what the navigation_layers bitmasks are for. They are polygon filters for the path queries. That way you can have one region that provides different access for a variety of agents without needing to change the entire polygon layout.

Happy New Year!

Thank you for your response. You are right, I was able to make it work with switching layers. Documentation also says this is better performance wise.

However, my main pain point is not this logic but rather manual setup of regions on the map. I still need to create 3 regions for each gate: before, after, gate itself. On top of that aligning edges of regions become a hard task when agent radius is >0, I have to move pixel by pixel, bake and see if they are aligned but not overlapping. This could work if I have a few gates but quickly become problems when building more levels for game and need to change levels often.

I was hopping to have something like NavigationObstacle2D but with ability to set layers on it to define who “sees” that obstacle.

One of my ideas is to do baking myself and create multiple regions from code but that seems like over engineering.