Create a PointGraph and Node from Editor script and then failed to compute path (from editor script too)

  • A* version: 4.2.17
  • Unity version: 6000.2.0f1

I am using the free version as I wanted to test it first but now I realize it is quite old compared to the pro.
Here is my problem: I am trying to generate a PointGraph and all node and connection from editor script.

I am scene I have a GameObject “AStar” whith a Pathfinder component and no graphs at this time.
The through a custom editor I run some code to create a PointGraph and some nodes and connections like this

    private void CreatePointGraph()
    {
        AstarPath aPath = AstarPath.active;
        if (aPath == null)
        {
            Debug.LogError("Cannot find active AStarPath");
            return;
        }

        AstarData data = aPath.data;

        m_PointGraph = data.FindGraphOfType(typeof(PointGraph)) as PointGraph;
        if (m_PointGraph == null)
        {
            m_PointGraph = data.AddGraph(typeof(PointGraph)) as PointGraph;
        }
    }

Then I create nodes and connections
Nodes is a list of object I already have which creates a graph I have for my procedural world (it is like a road network formed of points) and I want to create a PointGraph from it.

    private void PopulatePointGraph()
    {
        AstarPath aPath = AstarPath.active;
        if (aPath == null)
        {
            Debug.LogError("Cannot find active AStarPath");
            return;
        }

        aPath.AddWorkItem(() =>
        {
            for (int i = 0; i < Nodes.Length - 1; i++)
            {
                var node1 = Nodes[i];
                var anode1 = graph.AddNode((Int3)node1.Position);
                PointNodes[i] = anode1;

                var node2 = Nodes[i + 1];
                var anode2 = graph.AddNode((Int3)node2.Position);
                PointNodes[i + 1] = anode2;

                var cost = (uint)Mathf.Round(LengthToNextNode[i]);

                anode1.AddConnection(anode2, cost);
                anode2.AddConnection(anode1, cost);
            }
        });

        // not sure what is the best way to save/flush/update the graph.

        aPath.FlushWorkItems();
        aPath.FlushGraphUpdates();
        aPath.Scan();

        var data = AstarPath.active.data;
        data.SetData(data.SerializeGraphs());
        EditorUtility.SetDirty(AstarPath.active);
    }

And then I am trying to compute a path (not called directly after but when I click on a button)

    public void ComputeTestPath()
    {
        AstarPath aPath = AstarPath.active;
        if (aPath == null)
        {
            Debug.LogError("Cannot find active AStarPath");
            return;
        }

        var start = way.Nodes[0];
        var end = way.Nodes[way.Nodes.Length - 1];

        var info = AstarPath.active.GetNearest(start.Position);
        Debug.Log(info + " " + info.node + " " + info.position);

        var path = ABPath.Construct(start.Position, end.Position, (Path p) =>
        {
            Debug.Log("Path completed: " + p.vectorPath.Count);
            foreach(var vp in p.vectorPath)
            {
                Debug.Log(vp);
            }
        });

        AstarPath.StartPath(path);
    }

and neither GetNearest and Construct/StartPath seems to work.

I know I am not trying to make something conventionel but wanted to know if there is a way to do it or not ?

Note that the PointGraph don’t have any root set.
Not sure I am ultra clear, I hope you can help me

Another point: I read this post (Procedural graph generation in edit mode), look a bit similar to me, tryied the solution if found but it does not help me (sorry as a new user I cannot post link)

I make procedural maps and graphs in the Editor and runtime with the same code.

1)Destory and create new to make sure it clean

public void Setup() {
      AstarData data = AstarPath.active.data;
      var scanGraphs = aStarPath.graphs.ToList().FindAll(g => g is PeakPointGraph).ToArray();
      if (scanGraphs.Length > 0) {
         data.RemoveGraph(scanGraphs[0]);
      }
      peakPointGraph = data.AddGraph(typeof(PeakPointGraph)) as PeakPointGraph;
      peakPointGraph.optimizeForSparseGraph = false;
   }

then scan

 var scanGraphs = new NavGraph[] {peakPointGraph};
      aStarPath.Scan(scanGraphs);

then call workitem

 AstarPath.active.AddWorkItem(new AstarWorkItem(() => { 

//add nodes & connections here
}
 AstarPath.active.FlushWorkItems();

then do path creation and it works.

I just tested moving and removing the Scan() and my code works fine, but my Scan does nothing. I think that is normal for PointGraphs.
How do nodes get added to your PointGraph?
If you check ShowGraphs in the inspector for your AstarPath Monobehaviour, do you see any nodes? You Should use nodeCount not Nodes.Length, for effecientcy the Array size often does not match the number of nodes. Can you put a Debug.Log() to show nodeCount in PopulatePointGraph() and ComputeTestPath(). You may not have any nodes in the graph.

Thanks. I’ll try this latter today (personal and currently just started my day at work).

Ok
so I tried with hardcoded position and looks like it is working.
what is strange is that I can view the connectin between node (blue line) in the scene view but cannot see the node gizmo (get it be because I don’t set a root transform to the PointGraph ?).

And then I am able to compute a path
but this I am using Path.path instead of Path.vectorPath

But I still have an issue when I am testing with my real data but should be my fault. I will investigate. thanks for the help.

Ok finally found the issue in my code.
I was creating some node in double so it completly broke my connection.
thanks for the help

1 Like