Using gridgraph like a tilemap

Hi there!

I just discovered the librairy and I must say, it looks fantastic!

My goal is to use strictly the code part of it. I want to have a grid and decide which nodes are obstacles and etc myself.

Here is the code I have so far:

`
void InitAStar()
{
// This holds all graph data
_aStar = AstarPath.active;

    if (_aStar == null)
        Debug.Log("Astar is null");

    _aStarData = _aStar.astarData;

    // This creates a Grid Graph
    _gridGraph = _aStarData.AddGraph(typeof(GridGraph)) as GridGraph;

    // Setup the grid graph
    _gridGraph.width = nbX;
    _gridGraph.depth = nbZ;
    _gridGraph.nodeSize = tileW;
    _gridGraph.center = new Vector3(0, 0, 0);

    // Updates internal size from the above values
    _gridGraph.UpdateSizeFromWidthDepth();

    // Scans all graphs, do not call gg.Scan(), that is an internal method
    AstarPath.active.Scan();
}

`

This part seems to work, and creates a visualization of my GridGraph on the scene with all red squares.

My problem is with accessing and changing a specific node.
The code I have doesn’t seem to work:

`
public void SetTile(int x, int z, bool walkable, uint tag, uint penalty)
{
Vector3 nodePosition = new Vector3(x, 0, z);

    GraphNode node = _aStar.GetNearest(nodePosition).node;

    if (node != null)
    {
        node.Penalty = penalty;
        node.Walkable = walkable;
        node.Tag = tag;

        GraphUpdateObject guo = new GraphUpdateObject( new Bounds( nodePosition, new Vector3(2f, 2f, 2f) ));
        AstarPath.active.UpdateGraphs(guo);
    }
    else
    {
        //Debug.Log("Cannot set node [" + x + "," + z + "] doesn't exist!");
    }
}

`

This part doesn’t seem to work. Also, am I providing the GetNearest() method with the scene transform position of the nodes or their index on the grid itself?

It seems odd to me that I have to use a search function even though I know which node I want.

Could anyone help me please?

Hi

The GraphUpdateObject will essentially revert the change you just did.
What I think you should do instead is to override the grid graph.

Essentially

 public class MyGridGraph : GridGraph {
    public override void UpdateNodePositionCollision (GridNode node, int x, int z, bool resetPenalty = true) {
        node.Penalty = 5600;
        node.Walkable = true;
        // ...
  
        // Line needed for erosion and graph updates to work correctly
        node.WalkableErosion = node.Walkable;
 }

Or you could loop through all nodes in the graph

 var gg = AstarPath.active.astarData.gridGraph;
 for ( int z = 0; z < gg.depth; z++ ) {
     for ( int x = 0; x < gg.width; x++ ) {
          GridNode node = gg.nodes[x + z*gg.width];
          // do stuff
     }
 }
 
 // Line needed for pathfinding to work correctly
 AstarPath.active.FloodFill ();