Adding PointNodes to multiple platforms

Hello everyone, I’m trying to implement the PointGraph system for my 2D platform game. But I’m stuck on the following problem:

I have the following function to create point nodes:

 public void createPathNodes()
{
    AstarPath.RegisterSafeUpdate(delegate ()
    {
        Vector3 topLeftNode = new Vector2(transform.position.x, extents.y);
        Vector3 topRightNode = new Vector2(extents.x, extents.y);

        pointGraph.AddNode((Int3) topLeftNode);
        pointGraph.AddNode((Int3) topRightNode);
        Debug.Log("counter: " + pointGraph.CountNodes());
        leftNode = pointGraph.nodes[pointGraph.nodeCount - 2];
        rightNode = pointGraph.nodes[pointGraph.nodeCount - 1];
        leftNode.AddConnection(rightNode, 0);
        rightNode.AddConnection(leftNode, 0);

        float d = 0f;
        if (pointGraph.IsValidConnection(leftNode, rightNode, out d) == true)
        {
            Debug.Log(d);//prints ~0.019
        }

        if (isPlatformLeft) {
            platformLeft.connectRightNode(leftNode);
            
        }
        if (isPlatformRight) {
            platformRight.connectLeftNode(rightNode);
            Debug.Log("right");
        }
    });

    AstarPath.active.Scan();

}

The problem is that only 2 point nodes are created (on 1 platform) while this script is attached to multiple platforms. The debug counter also shows that only 2 nodes are created although there should be 2 nodes for each platform.
I’ve tried to fix this problem for awhile now, but it’s quite hard, especially with the lack of pointGraph examples.

Any help is appreciated!

Hi

I’m not seeing any place where you add nodes to multiple platforms. That method only seems to add two nodes, not multiple ones. If you are running that method once for each platform, note that calling AstarPath.active.Scan will clear all graphs and recalculate them from the start.

The method is indeed running for every platform in the scene.

I’m quite unsure on how to use the AstarPath.active.Scan function now (or whether I should use it). If I use the scan function does that mean I will lose all the current nodes? How should I ‘activate’ the nodes I created in the method?

Hi

The Scan method will clear all graph nodes and recalculate them from scratch. What you likely want to do is to run that script for all platforms (which registers a lot of safe updates) and then run AstarPath.active.FlushThreadSafeCallbacks() to make them run immediately. Don’t run AstarPath.active.Scan at any time.

1 Like

Oh, and also. After you have called FlushThreadSafeCallbacks, run AstarPath.active.FloodFill, otherwise pathfinding will not work on the nodes.

1 Like

Thanks! This seems to work. There are now two nodes for every platform in the scene.

The pathfinding is having some issues though: it seems to select a waypoint that’s above the target’s position.
See this gif I made: https://i.gyazo.com/f98805864c69cace66b82241c594c230.gif, ‘Player 2’ is trying to get to ‘Player 1’.

Do you have any clue what could be the cause of this?