A* Pathfinding Project

Graph Scan resets penalties


#1

Hi,

I’m using the latest version of A* Pathfinding Project Pro and in the project want to during runtime add a penalty of some grid nodes for few seconds and then remove it.

For adding:

        GridGraph currentGridGraph = (GridGraph)AstarPath.active.data.graphs[0];
        GridNode node = (GridNode)currentGridGraph.GetNearest(thisTransform.position).node;

        AstarPath.active.AddWorkItem(() => {
            node.Penalty += 1000;
            currentGridGraph.CalculateConnections(node.XCoordinateInGrid, node.ZCoordinateInGrid);
        });

For removing:

        AstarPath.active.AddWorkItem(() => {
            node.Penalty = (uint)Mathf.Clamp(node.Penalty, 0, node.Penalty - 1000);
            currentGridGraph.CalculateConnections(node.XCoordinateInGrid, node.ZCoordinateInGrid);
        });

And this works fine as I can see it in Debug(Penalty). Problem is that I’m also changing walkability of some nodes during runtime and when I Scan graph using

currentGridGraph.Scan();

all penalties get reset immediately.

Is this desired behavior or I’m doing something wrong?


#2

Hi

A scan recalculates the graph from scratch, so it will reset the penalties (if fact it resets everything).
If you want to just make an update to the graph you can use a graph update instead: see https://arongranberg.com/astar/docs/graph-updates.php

This will by default reset penalties as well, but you can disable that (see below)

// As an example, use the bounding box from the attached collider
Bounds bounds = GetComponent<Collider>().bounds;
var guo = new GraphUpdateObject(bounds);

// Set some settings
guo.resetPenaltyOnPhysics = false;
AstarPath.active.UpdateGraphs(guo);

#3

Hi Aron,

Thanks for the quick reply! :slight_smile:

Since my node size in the grid is a constraint for movement I don’t have any other bounding box, just want to set the whole node walkable or not. Is this also valid if I use in this case to update graph?

        node.Walkable = true;
        currentGridGraph.CalculateConnectionsForCellAndNeighbours(node.XCoordinateInGrid, node.ZCoordinateInGrid);

or in case of node what would be bounding box if GraphUpdateObject is the way to go?


#4

Hi

Yes, that code would work perfectly fine. If you are just updating a single node that’s a better way than doing it using a GraphUpdateObject.