Best way to get a duplicate of a graph and assign it to a seperate graph layer/index?

Hi have been customizing this for our game.

Just wanted to know the best way to create a duplicate of a grid graph eg
hard copy AstarPath.active.graphs[1] to AstarPath.active.graphs[3] (at runtime/ level load);

So i can then add the special connections to the new instance and choose which grid the ai should use based upon its capabilities.

      void ConnectJumpSpot(int graphNumber)
                {
                    var graph = AstarPath.active.graphs[graphNumber];
                    GraphNode startNode = graph.GetNearest(from, NNConstraint.Default).node;
                    GraphNode toNode = graph.GetNearest(to, NNConstraint.Default).node;
                    startNode.AddSpecialAction(GraphNodeSpecialAction.JumpForwards, jumpPoint);
                    startNode.AddConnection(toNode, 0);
                    jumpPoint.FromNode = startNode;
                    toNode.AddSpecialAction(GraphNodeSpecialAction.JumpBackwards, jumpPoint);
                    toNode.AddConnection(startNode, 0);
                    jumpPoint.ToNode = toNode;
                }

This is for a pro layered grid graph.
Have already edited the base code to work with the special actions in aipath.

Cheers

1 Like

Hi

It’s not super easy to duplicate a graph unfortunately. The easiest way I think is to serialize the graph to a file and then load it using AstarPath.active.data.DeserializeGraphsAdditive. See https://www.arongranberg.com/astar/docs/save-load-graphs.php

Excellent, should be easy to get a byte representation of a graph and load it into another index with these functions. Cheers.