Updates nodes on all Graphs

Hi,

I am trying to update my grid graphs when I lay down some road.
I use this code from the documentation.

AstarPath.active.AddWorkItem(new AstarWorkItem(() => {
// Safe to update graphs here
var pathNode = AstarPath.active.GetNearest(roadNode.worldPosition).node;
pathNode.Penalty = 0;
}));

I have two Grid graphs in my scene on top of each other. It works perfectly for the first graph but the second graph is not touched by it at all. How do I update both graphs?

Hi

You can pass in an NNConstraint indicating which graph you want.

var nn = NNConstraint.None;
nn.graphMask = 1 << 0; // Only graph index 0

AstarPath.active.GetNearest(p, nn);

Thanks for the reply.

Why doesn’t it update graph number two when I do I like this.

  NNConstraint n = NNConstraint.None;

  n.graphMask = (1 << 0) | (1 << 1);

AstarPath.active.AddWorkItem(new AstarWorkItem(() => {
// Safe to update graphs here
var pathNode = AstarPath.active.GetNearest(node.worldPosition, n).node;
pathNode.Penalty = 0;
}));

But it does work when I do it like this (Double work for the CPU’s I guess)

                NNConstraint n = NNConstraint.None;

                n.graphMask = 1 << 0;

                AstarPath.active.AddWorkItem(new AstarWorkItem(() => {
                    // Safe to update graphs here
                    var pathNode = AstarPath.active.GetNearest(node.worldPosition, n).node;
                    pathNode.Penalty = 0;
                }));

                NNConstraint nn = NNConstraint.None;

                nn.graphMask = 1 << 1;

                AstarPath.active.AddWorkItem(new AstarWorkItem(() =>
                {
                    // Safe to update graphs here
                    var pathNode = AstarPath.active.GetNearest(node.worldPosition, nn).node;
                    pathNode.Penalty = 0;
                }));

Because you are only getting the closest node to that position, out of all nodes on all graphs.
Since you want to update the closest point on each graph, you will need to loop over the graphs.