RemoveConnection seemly not Working

I am trying to remove the connection between two nodes on a hex grid graph. Calling RemoveConnection does not seem to work, while ClearConnections does. AddConnection also seems to work as expected. By “work” I mean both the visualization of the connections and the path taken by agents. I can remove all connections and then add the needed ones back in, but that seems pretty hacky.

What am I doing wrong? Did I miss a detail?

I’m using Unity 2020.3.X and version 4.2.17 of Pathfinding.

    AstarPath.active.AddWorkItem(new AstarWorkItem(ctx =>
    {
        var node1 = AstarPath.active.GetNearest(this.transform.position + this.transform.forward).node;
        var node2 = AstarPath.active.GetNearest(this.transform.position - this.transform.forward).node;

        if (node1.ContainsConnection(node2))
            Debug.Log("Connection exists"); //true

        node1.RemoveConnection(node2);

        if (node1.ContainsConnection(node2))
            Debug.Log("Connection exists"); //true

        node2.ClearConnections(true);

        if (node1.ContainsConnection(node2))
            Debug.Log("Connection exists"); //false
    }));

Ok. I saw that RemoveConnection does NOT work for grid graphs. Rather we have to use SetConnectionInternal which seems to be working. If there is another solution to this I’d love to hear it - using RemoveConnection seems much simpler than trying to figure out the direction.

This is what I’ve had to do to remove a connection.

        var node1 = AstarPath.active.data.gridGraph.GetNearest(tile1).node as GridNode;
        var node2 = AstarPath.active.data.gridGraph.GetNearest(tile1).node as GridNode;

        node1.SetConnectionInternal(2, false);
        node2.SetConnectionInternal(1, false);

Seems to be an optimization: Compiler Directives - A* Pathfinding Project

I appreciate the effort the dev has put in this project, but having no way to disable this in the free version (as it’s a pretty big feature hit) is disappointing.

The code for it is in GridNodeBase.cs and GridNode.cs, it might be possible to edit the source files to enable the behavior. If I figure it out I’ll update this post.

I’ve done a bit more work with it. It seems that what I have above works (second post), but also causes a recalculation of the local area. I was intending to put in “walls” along the hexagon edges. This works for one edge at a time, but the connections seem to re-appear if an additional nearby connection is removed.

For now, I’m looking at changing the size of the nodes, putting the walls on the obstacle layer, and adjusting the grid accordingly. In some ways better in some ways not.

If you figure something else out I’d love to see it.

Edit: Maybe I’m wrong. Maybe it doesn’t recalculate. I need some more testing.

So I put this in start() of one of my scripts

#if ASTAR_GRID_NO_CUSTOM_CONNECTIONS
        Debug.Log("ASTAR_GRID_NO_CUSTOM_CONNECTIONS IS TRUE");
#else
        Debug.Log("ASTAR_GRID_NO_CUSTOM_CONNECTIONS IS FALSE");
#endif

And it came back false, which means everything should be working. Something else must be wrong with it.

I’ve got it working now. I must have had an error earlier.

Using SetConnectionInternal works well. The challenge was figuring out the numbering of the direction, but using a little trial and error wasn’t too bad.

Hi

This is indeed a wart in the current api. I’ve changed this locally now so that RemoveConnection also works for grid connections. That’s really what it should have done all along.
The change will be included in the next 4.3 beta.

Thanks Aron! The remove connection function looks much easier to use.

1 Like