Pathfinding and fog of war

Hello! As the title implies, are there any general ideas as to how to integrate fog of war with A* pathfinding? My biggest issue/fear is units knowing the entire map by heart even though they’ve “seen” only a small portion of it. So the player can order the units to go to the opposite end and they would know the best path immediately.

I suppose one way to do it would be to mask unrevealed game objects out of the pathfinding calculations so that the units will try a straight line until running into an obstacle. Is this kind of masking possible? Also I fear that this would be quite performance-intensive, checking for a new path whenever a new object is revealed. Or I don’t suppose it would be possible to see if a newly revealed object is in the way of the current path and only recalculate if that is the case?

Are there any other “standard” solutions to this problem? Maybe something obvious I’m missing?

Cheers!

You could use TagMask to mark nodes.

But reconsider if you seriusly need it.
Take a look at starcraft, you can order units to go into FOW and it is not a problem, actually it would be a problem if you could not do it.

Hi

The easiest way is probably to subclass the grid graph and add in your check for if the cell is visible or not:

You can subclass it like this:

using pathfinding;

public class MyGridGraph : GridGraph {
	public override void RecalculateCell (int x, int z, bool resetPenalties = true, bool resetTags = true) {
		base.RecalculateCell(x, z, resetPenalties, resetTags);
		var node = nodes[z*width + x];
                // Modify the node here somehow
                if (node is not visible in fog of war) node.Walkable = true;
               
               // Need to set this if you modify the walkability in any way, otherwise erosion will not work
               node.WalkableErosion = node.Walkable;
	}
}
// In an editor script
using pathfinding;

[CustomGraphEditor(typeof(MyGridGraph), "My Grid Graph")]
public class MyGridGraphEditor : GridGraphEditor {
}

Then you can use a graph update to update the grid wherever the units are or slightly more performantly: wherever the FOW changes.

Here is a (really old) video of me using this trick https://www.youtube.com/watch?v=6HcMYCS08XQ