Support Forum

How to deal with 'separate'/additive graphs (dynamically un/load)?

Hi,
I’m trying to plan out how to integrate pathfinding for a multi-scene server setup.
Ideally the pathfinder would not be a singleton, but as this will probably not change, i want to see if there are alternatives.
Rooms on the server are loaded and unloaded as required (using additive scenes) and ideally i want to handle pathfinding for each room separate from the others, as there’s no dynamic crossing of boundaries (just ‘teleports’). It looks like there’s DeserializeGraphsAdditive() which i could use to load ‘a graph’ (/graphs) for a specific room and DestroyGraph to remove ‘it’ again, but there’s no Serialize(Single)Graph to begin with nor corresponding editor tools to deal with separate graphs.
In an ideal world i would bake a single graph inside each room’s scene and only locally manage it inside that scene too (i only require direct path queries, without seeker); Currently it seems i would have to load all scenes for rescanning even.
Any tips or hints about possible workflows for what i’m trying to accomplish?

I think i found two options:

  1. Use scene-local pathfinder singletons, just for creating the graphs and de/serialize them from this disabled instance into the global one on load (awkward, but preferred editor workflow alternative).
  2. Create editor scripts to temporarily load a ‘manager’ scene that contains the pathfinder singleton and re-scan a specific graph linked to the edited scene.

It would be nice if graph management was not linked so tight to the pathfinder before usage.

Any ideas for other options are still appreciated

The first option doesn’t seem to be possible as Awake is called even on disabled scripts.
Another option i think may be to export a navmesh and create the graph at runtime from it (although still not ideal for editing).

It would even help if the system allowed a ‘singleton’ per scene instead of per game.

Hi

Yeah, having it be a singleton was a bad design decision made very early in this package’s history. I would like to change it, but it’s pretty much impossible to do at this point while keeping backwards compatibility.

It’s not possible to serialize an individual graph using the AstarData class because a graph may reference other graphs via off-mesh links and similar. Your graphs might not do this, but the AstarData class doesn’t know this.

However, if you really want to serialize a single graph, you can do it using:

var settings = new SerializeSettings {
    nodes = true,
};
var graphLock = AstarPath.active.PausePathfinding();
var sr = new AstarSerializer(null, settings, AstarPath.active.gameObject);
sr.OpenSerialize();
var graphs = new List<NavGraph> {
    AstarPath.active.graphs[0],
};
sr.SerializeGraphs(graphs);
sr.SerializeExtraInfo();
byte[] bytes = sr.CloseSerialize();
graphLock.Release();

That atleast gives me an option. Thanks