A* Pathfinding Project

Can't disconnect grid nodes


#1

I want disconnect GridGraph’s node connections by code.

but this not works.

 private void DisconnectCenterNodes()
    {
        AstarPath.active.AddWorkItem(new AstarWorkItem(ctx =>
        {
            var gridGraph = AstarPath.active.data.gridGraph;
            for (var z = 0; z < 3; z++)
            {
                for (var x = 0; x < 3; x++)
                {
                    var node1 = gridGraph.GetNode(x, z);
                    var node2 = gridGraph.GetNode(x, z + 1);
                    Debug.Log((Vector3)(node1.position));
                    Debug.Log((Vector3)(node2.position));

                    node1.RemoveConnection(node2);
                    node2.RemoveConnection(node1);
                    node1.SetConnectivityDirty();
                    node2.SetConnectivityDirty();
                    Debug.Log("disconnect!");
                }
            }

            gridGraph.GetNodes(node => gridGraph.CalculateConnections((GridNodeBase)node));

        }));
    }


#2

Hi

Yeah, that’s badly documented (I have fixed that now). The RemoveConnection method only removes custom node connections (i.e. those added by link components or the AddConnection method). To remove grid connections you must use the SetConnectionInternal method.

e.g.

var node1 = gridGraph.GetNode(x, z) as GridNode;
node1.SetConnectionInternal(2, false);

The node directions are laid out as follows

         Z
         |
         |

      6  2  5
       \ | /
 --  3 - X - 1  ----- X
       / | \
      7  0  4

         |
         |

See https://arongranberg.com/astar/docs/gridnode.html#SetConnectionInternal


#3

thank you. this works. but have problems still.

my game use grid graphs, and have structure like SimTower game.

same floor tile connect, different floor tiles not.

so i made manual grid control class. (for not connect vertical tiles automatically)

this is my summarized code.

 public class ManualPathGridControl
    {
        public void ManualScan4Way()
        {
            for (var x = 0; x < _chunkQuery.MapWidth; x++)
            {
                for (var y = 0; y < _chunkQuery.MapHeight; y++)
                {
                    var selfCoord = new Coord(x, y); 
                    ConnectOrDisConnectByWalkableWhenAble(selfCoord, selfCoord.Up);
                    ConnectOrDisConnectByWalkableWhenAble(selfCoord, selfCoord.Down);
                    ConnectOrDisConnectByWalkableWhenAble(selfCoord, selfCoord.Left);
                    ConnectOrDisConnectByWalkableWhenAble(selfCoord, selfCoord.Right);
                }
            }
        }

        private void ConnectOrDisConnectByWalkableWhenAble(Coord selfCoord, Coord neighborCoord)
        {
            bool selfWalkable = _mapQuery.GetWalkable(selfCoord);
            bool neighborWalkable = _mapQuery.GetWalkable(neighborCoord);
            if (selfWalkable && neighborWalkable)
            {
                ConnectNodeWhenAble(selfCoord, neighborCoord);
            }
            else
            {
                DisConnectNodeWhenAble(selfCoord, neighborCoord);
            }
        }

        public void ConnectNodeWhenAble(Coord coordFrom, Coord coordTo)
        {
            AstarPath.active.AddWorkItem(new AstarWorkItem(() =>
            {
                PathNode nodeFrom = _astarPathQuery.GetNode(coordFrom);
                PathNode nodeTo = _astarPathQuery.GetNode(coordTo);
                var directionFrom = GetNodeConnectionDirection(coordFrom, coordTo);
                var directionTo = GetNodeConnectionDirection(coordTo, coordFrom);
                nodeFrom.SetConnectionInternal(directionFrom, true);
                nodeTo.SetConnectionInternal(directionTo, true);
            }));
        }

        public void DisConnectNodeWhenAble(Coord coordFrom, Coord coordTo)
        {
            AstarPath.active.AddWorkItem(new AstarWorkItem(() =>
            {
                PathNode nodeFrom = _astarPathQuery.GetNode(coordFrom);
                PathNode nodeTo = _astarPathQuery.GetNode(coordTo);
                var directionFrom = GetNodeConnectionDirection(coordFrom, coordTo);
                var directionTo = GetNodeConnectionDirection(coordTo, coordFrom);
                nodeFrom.SetConnectionInternal(directionFrom, false);
                nodeTo.SetConnectionInternal(directionTo, false);
            }));
        }
    }

when i run this, makes too much divided zones. (1 zone per grid)

and very slow.

what do you think I do?

some advice please.

(in this video not show performance problem, but can help understand path grid usage)