Hi Team!
Is there a way to just feed in data given a grid (i.e. 1 array of points positions, 1 array of obstacles positions) to Astar and then have it still generate paths etc using just the data?
update: is this the right page to look at to answer my question? Creating graphs during runtime - A* Pathfinding Project
Hi
That page is definitely relevant. Then you’ll probably also want GridGraph.SetWalkability, which you can call after scanning the graph.
so i am doing the following to create a pointgraph (following example from the url)
AstarData data = AstarPath.active.data;
PointGraph graph = data.AddGraph(typeof(PointGraph)) as PointGraph;
AstarPath.active.Scan(graph);
for (int i = 0; i < points.Count - 1; i++)
{
AstarPath.active.AddWorkItem(new AstarWorkItem(ctx =>
{
var node1 = graph.AddNode((Int3)points[i]);
var node2 = graph.AddNode((Int3)points[i + 1]);
var cost = (uint)(node2.position - node1.position).costMagnitude;
GraphNode.Connect(node1, node2, cost);
}));
AstarPath.active.FlushWorkItems();
}
foreach (var node in graph.nodes)
{
node.Walkable = true;
}
not sure how to check if the above was successful but when i played in Editor, i can see a blue debug showing the points and the connection so I assume its correct.
i tried to create path to test it using
AstarPath.active.Scan();
var path = ABPath.Construct(points[0], points[points.Count - 1], null);
Debug.Log(path.vectorPath.Count);
but this will return 0.
is there something I am missing?
i didn’t do SetWalkability because i was creating a pointgraph
ah ok i realized why. because the graph is duplicating nodes and thus there’s no connection. so if i had an array of 4 and use this for loop, 0->1a but then i created another node 1b->2. Thus when i try to path from 0 to 4, there’s no path since 1a is not connected to 2…
i fixed this by looping through the created nodes and comparing them against each other and if distance calculated is a certain value, connect them to each other
another question: i tried doing this just to add nodes
for (int i = 0; i < points.Count; i++)
{
// Make sure we only modify the graph when all pathfinding threads are paused
AstarPath.active.AddWorkItem(new AstarWorkItem(ctx =>
{
graph.AddNode((Int3)points[i]);
}));
// Run the above work item immediately
AstarPath.active.FlushWorkItems();
}
i have 5 points in the initial array to add but for some reason, the final count of nodes from the graph is 8 and when i loop through, the last 3 nodes are nulls. Is there a reason why these empty nodes are created and added?