How to create, modify and connect?

Hello, long term user of the Pro version, purchased the initial one and upgrade thereafter.
I have a few questions, found a few answers to similar questions via the forums, but either they were deprecated or didn’t seem to work. Here goes :

  1. How can I create a new graph via code, and tell it’s location and size via code ?
  2. How can I change the size and location of an existing graph via code ?
  3. I need to connect two graphs through points ( let’s each graph is covering a room ), how could I do this ?

I’m using 2D grid graphs ( xy, not navmesh ).
Thanks for any answer :slight_smile: .

Hi

I’m glad to have you here for such a long time :slight_smile:

Something like

AstarPath.active.AddWorkItem(() => {
    var graph = AstarPath.active.data.AddGraph(typeof(GridGraph)) as GridGraph;
    graph.center = Vector3.zero;
    graph.rotation = new Vector3(-90, 270, 90);
    graph.collision.heightCheck = false;
    var nodeSize = 1.0f;
    graph.SetDimensions(100, 100, nodeSize);
});
AstarPath.active.FlushWorkItems();
AstarPath.active.Scan();
// or graph.Scan(), if you want to scan only this graph

If you want to discard the existing nodes, use code similar to the code above. If you want to keep the existing nodes, but move them. Then use something like this:

AstarPath.active.AddWorkItem(() => {
    var graph = AstarPath.active.data.gridGraph;
    // Note that the two last parameters have default values, so you can skip them if you want
    graph.RelocateNodes(newCenter, newRotation, newNodeSize, newAspectRatio = 1.0f, newIsometricAngle = 0.0f);
});
  1. If you know the points you want to connect, you can do something like this:
AstarPath.active.AddWorkItem(() => {
    var node1 = AstarPath.active.GetNearest(point1).node;
    var node2 = AstarPath.active.GetNearest(point2).node;
    var cost = (node1.position - node2.position).costMagnitude;
    node1.AddConnection(node2, cost);
    node2.AddConnection(node1, cost);
});

Usually I recommend using a single large graph if possible though. This avoids a lot of complexity.

Hi Aron, thanks for the reply
While using the code you provided to create a graph (1), I get this :

Exception: Processing work items recursively. Please do not wait for other work items to be completed inside work items. If you think this is not caused by any of your scripts, this might be a bug.
Pathfinding.WorkItemProcessor.ProcessWorkItems (Boolean force) (at Assets/AstarPathfindingProject/Core/Misc/WorkItemProcessor.cs:209)
AstarPath.PerformBlockingActions (Boolean force) (at Assets/AstarPathfindingProject/Core/AstarPath.cs:805)
AstarPath.FlushWorkItems () (at Assets/AstarPathfindingProject/Core/AstarPath.cs:1025)
AstarPath.OnDestroy () (at Assets/AstarPathfindingProject/Core/AstarPath.cs:1273)

The way I’m doing things is :

  • I create a GameObject to which I add : AstarPath ( no graphs ) , RVOSimulator and RVONamvesh. In the Awake() method of a different GameObject I load the level (via Resources.Load for now, will do it via asset bundles later), then I try to create the graph depending on the dimensions of the newly loaded level. I’m using v4.1.1.

Hi

That’s odd. The stacktrace says it’s coming from when the AstarPath object is destroyed. Are you destroying it at some point? Are you sure there are no exceptions before that point?

Hi,
No, not destroying it, the A* graph is there during runtime.
https://ibb.co/fzxReU
There is no prior exception, and I’m not using any other code to deal with it except for the one you provided.
After that exception, I get the exception that there is no Grid, and after about 100 of those, what follows is the same exception as above ( Processing work items recursively.).
Moved the code into “Start();” , same outcome.

Are you sure you are not creating multiple A* objects then, or that one is being destroyed when you load the new scene? Because according to the stacktrace you posted, at least one AstarPath component is being destroyed.

I tried to run the code myself in an empty scene with just an AstarPath object and a script that runs the code in (1), and it worked without any issues.

Hi Aron, I added a script that finds any AstarPath in the scene, and that also gives me a message if the gameObject is destroyed, and that’s the only one existent, and it doesn’t get destroyed or replaced.
https://ibb.co/eYQezU
I’ll try updating to the latest version and reimplementing it, and if that doesn’t work, I’ll try to work with preset AstarPath prefabs for different map dimensions, however I’d prefer to work with setting it up from code.
Thanks a ton for your input ! really appreciated

Oh, that’s a different exception. That one was a lot more useful.
Yeah, looking at the code for 4.1.1 there is actually one important change compared to the latest version. In 4.1.1 graphs cannot be added from work items, that’s why it’s throwing exceptions. In the latest version that is possible however.

Also. You may be interested in loading settings from files. See this page: https://www.arongranberg.com/astar/docs/save-load-graphs.php

Hi Aron, updated to the latest version. Now the errors are gone, the graph gets created, however the units don’t seem to use it.
If I create the graph manually, it works great.
Here is a comparison between the two.
https://ibb.co/gXvbyp

Thanks for the previous reply, I’m currently working with loading grid settings then scanning, and that works fine as well.

Hi

Since you are making a 2D game you should probably enable 2D physics too. That might be why.

graph.collision.use2D = true;

Otherwise, have you looked that the graph in the scene view and checked that it looks good?