Decoupling characters from navigation layer with AStarGrid2D, with multiple characters

Godot Version

v4.6.2.stable.official

Question

Hello!

I was wondering if there is a good way to decouple my Character class from my NavigationLayer class, which contains a single AStarGrid2Dinstance that should update to forbid characters from moving through each other.

The straightforward implementation that I currently have passes the AStarGrid2D instance to each Character instance, and the character uses its functions for finding paths and for setting its final position as solid in the AStarGrid2D. This means that the grid is changed directly in the Character class, which doesn’t seem the best practice to me, as I’d prefer greater separation and for the grid to not be changed in places that might be hard for me to keep track of.

A straightforward improvement that I can think of would be passing the instance of NavigationLayer to the characters, and Character calling functions from NavigationLayer to update the AStarGrid2D.

However, this still seems unsatisfactory to me, as Character making such changes to the AStarGrid2D in this manner keeps the Character and NavigationLayer grid coupled.

I was thinking that I could use signals between Character and NavigationLayer to update the AStarGrid2D, but while I could use signals to also ask for the path and send it back, this already seems a bit overly complicated. On the other hand, if I already pass the instance of the AStarGrid2D to the instances of Character, using signals at all for changing the AStarGrid2D seems a bit unnecessary, as I could change the grid directly from Character.

So my questions are:

  1. Is there a proper way of doing this without passing the AStarGrid to each character?
  2. Are there actually problems with my current implementations or suggested improvement?
  3. What’s the best practice here, keeping in mind decoupling if possible?

Take a look at NavigationAgent2D. See how it is used and copy that interface.

I think what I did was have a node with the var astargrid2d and then the characters were children and I ran the logic from that node so it was easy to get a reference to the characters and move them around.