Setting GridGraph penalties resets penalties of neighbouring nodes

Hey there,

(I did search on the forum and couldn’t find a dupe, apologies if this was answered before)

I have a 200x200 GridGraph with nodes 5 units in size. The graph has all collision testing turned off, so it’s essentially a flat fully-connected grid where I can set penalties on individual nodes.

I set penalties like so:

public void updateGoAnywhereGraph(int penalty, WorldPosition wp) {

        MyGUO guo = new MyGUO();
        guo.setPenalty = penalty; // set to 200 * 1000 elsewhere
        guo.bounds = new Bounds(wp.position, new Vector3(10,5,10));
        Debug.Log("bounds is: "+guo.bounds);
        guo.nnConstraint.graphMask = GraphMask.FromGraphName("construction_worker");
        AstarPath.active.UpdateGraphs(guo);
    }

    public class MyGUO : GraphUpdateObject {
        public int setPenalty = 0;
        public override void Apply (GraphNode node)
        {
            base.Apply(node);
            node.Penalty = (uint) setPenalty;
        }
    }

The first time I run the update, it behaves as expected - the nodes which fall within the bounds get updated, so according to the gizmos I have a 2x2 patch of nodes coloured red, everything else green.

However, I then run the update on an immediately adjacent square of the graph. The update correctly sets the penalties of the new nodes, but one row of the immediately adjacent, and previously updated, nodes have their penalties reset! (at least, they turn green in the gizmos)

I can repro this along both X and Z axes. Any idea what’s going on?

Hi

By default, a GraphUpdateObject will recalculate all nodes from scratch that a collider with that size could potentially affect. If you just want to update penalties, you probably want to disable this. You can do that by setting guo.updatePhysics = false. Without this, it will first recalculate the nodes from scratch (zeroing penalties) and then set your specified penalties.

Wonderful, works like a charm! Thank you! I was hoping for a bool I could just flip :slight_smile:

FWIW - I was aware of updatePhysics, but discounted it since I’d turned off all of the colliders on the graph. I didn’t realise that a ‘blank’ updatePhysics would end up resetting everything about the nodes including penalties. Perhaps something to add to the docs?

Thanks again for the speedy reply!

1 Like