Agents won't work on randomly generated levels

I have this levels randomly generated, I have them filled with nodes.
Once the random level is done generating, I call this.


        //I have a persistent game object with a Point Graph,I get it here:
        var graph = AstarPath.active.data.pointGraph;

        //I get the different nodes scattered throughout the rooms
        m_nodes = m_root_dungeon.GetComponentsInChildren<II_PathNode>();

        //add them to the Point Graph
        AstarPath.active.AddWorkItem(new AstarWorkItem(ctx => {
            // Add 2 nodes and connect them
            for (int i = 0; i < m_nodes.Length; i++)
            {
                graph.AddNode((Int3)m_nodes[i].transform.position);
            }
            
            // Required because we have changed connectivity of the graph
            ctx.QueueFloodFill();
        }));

        //scan the new graph
        AstarPath.active.Scan(graph);

If I check the the number of nodes on the Point Graph it gives me the correct number, however the agents always return “Path Completed. Searched Nodes 0”.

The game also has levels that are not ramdonly generated, and I use the same agents there.
In those levels I load nodes precalculated with: AstarPath.active.data.DeserializeGraphs(m_astar_path.bytes); and there it works fine.

Any idea what I’m doing wrong? Below is a screenshot that shows a couple of rooms randomly generated, the purple spheres are the nodes and the Dragon is the agent. Thanks!

Hi

That will just add the nodes, however they are not connected with each other. If you want to use the PointGraph’s built-in connection calculations you can call PointGraph.ConnectNodes.

Thanks for the reply Aron.

Still not working as expected, It seems my nodes are not connected properly, I have a feeling I have a collider somewhere that is blocking them, because when the agent is stuck if I move it to another node manually then it reaches the goal fine. Is there a way to see visually the connections between those nodes? I’ve played with Debug section on the Astar Path component but I didn’t manage to display anything. Thanks again!

EDIT: I have found this code to draw the connections:

 var graph = AstarPath.active.astarData.graphs[0];
 graph.GetNodes(node => {
     node.GetConnections(other => {
         Debug.DrawLine((Vector3)node.position, (Vector3)other.position, Color.red);
     }
     return true;
 });

But it doesn’t display any connections. As I mentioned before this is a randomly generated level. If I get any room in the editor, add a Astar Path component to that specific room and Scan the nodes they get generated with no issues and they get displayed properly. So it seems I’m not creating properly the connections on the fly.

Also, for testing purposes I’m always creating the same two rooms, which have 21 nodes combined, however if I go to the main Astar Path component and click on the Point Graph info I get 73 nodes, probably the exceding nodes come from the previous level, is there a way to clean a graph before adding new nodes to it?

Got it working all fine after deleting the graph and creating a new one like so:

 AstarData data = AstarPath.active.astarData;
        NavGraph[] graphs = data.graphs;
        foreach (NavGraph graph in graphs)
            { data.RemoveGraph(graph); }
        

        data.AddGraph(typeof(PointGraph));

If you have “Show Graphs” enabled on the A* Inspector (at the bottom) then it should draw the connections in the scene view.

Odd that you had to recreate the graph… Can you reliably replicate that issue?

I touched many things while trying to fix it so it was probably something I did rather than a bug on Astar. To try and reproduce it now as it was when I was getting the problems I would have to revert some code and assets, I rather move on at this point :stuck_out_tongue: Thanks in any case for the help!

1 Like