Loading LocalSpaceGraph From File

Hi,

A bit of context - We are building a top-down game in which agents navigate inside the rooms of spaceships. There can be multiple ships on screen at once, and ships also move during gameplay (we have been using LocalSpaceGraph and LocalSpaceRichAI so bots can navigate on moving ships). The ships are regularly destroyed/spawned during play, so we have been saving a pre-defined graph as a .bytes file then loading the relevant graph when a ship is spawned (we’d like to avoid the performance hit of generating the graphs during run-time if possible).

The question - It seems loading a graph from file doesn’t contain a reference to the object on which the graph was generated, so LocalSpaceRichAI agents on a loaded graph don’t adapt to the movement of their parent object (the ship). Would anyone know how to save then load a LocalSpaceGraph from a file, so it can be applied to a moving object?

(If there is a better way of approaching this given the context above we’d also be very open to ideas!)

Thanks very much for the help!

Hi

Sorry for the late answer.
The LocalSpaceRichAI do not even know which graph they are on and never use that information. What they do is that they query for the closest point on any graph. What you probably want to do if you have many local space graphs is to set the Seeker -> Graph Mask to only allow pathfinding on the correct graph. If you are loading the graphs during runtime you probably have to configure this field during runtime as well as the indices may change. See https://arongranberg.com/astar/docs/accessingdata.html

Make sure you also assign the LocalSpaceRichAI.graph field to the correct LocalSpaceGraph component if you are instantiating agents during runtime.

Thanks for the detailed response! I have been playing around with this but still not managing to get it working.

The LocalSpaceRichAI are navigating the correct loaded graph, however if the LocalSpaceGraph object (the parent ship) moves, the navigation doesn’t update (so they’ll walk into walls if the wall is now over the original position of the graph). Essentially I believe it’s loading in a stationary graph rather than one that adapts to the movement of an object.

(I’m using this to load the graph)

AstarPath.active.data.DeserializeGraphsAdditive(navMeshGraph.bytes);

Is there any way to tell the deserialized graph that it should move with a specific LocalSpaceGraph object, or alternatively a way to tell the LocalSpaceGraph that it should use the deserialized graph as its base graph?

Thanks again!

Hi

The graph has no knowledge of that it is moving and does not require it. All the code related to the movement is handled in the LocalSpaceRichAI. You do however need to assign the LocalSpaceRichAI.graph field to the correct LocalSpaceGraph component.

Furthermore. When you create the new ships, they need to start at the exact same position in world space as when the graphs were generated. This is because the LocalSpaceGraph component will store the original position/rotation of the game object during the Start() function.