Penalties Disappear When Rescanning Part of a Graph

Hi,
I’ve got a 2D game that uses a GridGraph with an Initial Penalty of 800. Roads are marked with a penaltyDelta of -500, so my NPCs prefer to walk on roads. This works pretty well. However, when the player constructs something, a section of the grid is rescanned and the penalties disappear. (Player constructions add new 2DColliders which NPCs are not allowed to path through)

The roadway penalty is applied with a custom GraphModifier. I put a printline in the Apply method which is pretty simple:
public void Apply () {
Debug.Log("Applying pathfinding penalty from: " + this.gameObject.name);

			GraphUpdateObject guo = new GraphUpdateObject(GetBounds());
			guo.shape = compoundShape;
			guo.bounds = compoundShape.groupBounds;

			guo.addPenalty = penaltyDelta;

			AstarPath.active.UpdateGraphs(guo);
		}

It prints when I hit “Scan” in the inspector. However, when the player builds something, I call:

public void BuiltSomethingUpdatePathfinding(){
	Bounds b = affectedBounds;
	b.center = b.center + this.transform.position;
	
	var guo = new GraphUpdateObject(b);
	// Set some settings
	guo.updatePhysics = true;
	AstarPath.active.UpdateGraphs (guo);
}

In the areas where the “affectedBounds” overlap the GraphUpdateShape of the roads, the penalty is removed. Do I need to manually re-apply all the GraphModifiers? They are supposed to keep track of themselves but running an UpdateGraph seems to remove the penalties without calling any of the GraphModifier triggers (post scan, etc). If I manually re-apply my GraphModifiers, won’t the penaltyDelta be applied more than once in some areas?

Before screenshot:

Key: Red grid = high penalty pathfinding areas.
Green Grid (overlapping with the road) = low penalty pathfinding areas.
Red Squares = unwalkable areas, under colliders.
Salmon or Pink box = Construction area, this will be rescanned.

After constructing the fountain:

A probably unimportant detail: I do have a custom overloaded GraphUpdateShape to define the roads. Being able to override that was very helpful! I also overrode GraphModifier instead of using GraphUpdateScene. Since I only wanted to apply penalties, mine is very short and works in 2D.

Possibly important detail: I think there are occasionally missing areas due to the order my GraphModifiers are called in, it’s possibly the same problem of getting penalties reset between different GraphUpdates. Restarting Unity and rescanning seems to usually fix it.

Hi

By default when using ‘updatePhysics’=true it will reset the penalties to their initial values (possibly based on the terrain).
You can disable this by setting guo.resetPenaltyOnPhysics = false.

Woah, I can’t believe I missed resetPenaltyOnPhysics. I thought I had read through all the relevant code. Thanks!

Works great!