Support Forum

Growing empty array entries when using DeserializeGraphsPartAdditive

Hi all,
I’m currently integrating the Pathfinding Project into my project and it’s working great so far! :slight_smile:
Because I’m loading my Scenes additively in Unity I have one MainScene that contains my Pathfinding Component and whenever a new Scene is loaded I’m deserializing a “Blueprint” GridGraph and then update the bounds of it to match the newly loaded level.
This is my code that is executed when a new Scene is loaded:

pathfinding.data.DeserializeGraphsAdditive(_graphBlueprint);

GridGraph newGraph = (GridGraph)pathfinding.data.graphs.First(
    graph => graph is { name: "Graph Blueprint" }
);

newGraph.name = level.LevelId.StringValue;
newGraph.SetDimensions(
    (int)level.LevelSize.x,
    (int)level.LevelSize.y,
    newGraph.nodeSize
);
newGraph.center = level.LevelPosition + (level.LevelSize / 2F);

pathfinding.Scan();

When a Scene is unloaded I remove the Graph like this

foreach (NavGraph graph in pathfinding.data.graphs)
{
    if (graph == null || graph.name != sceneToUnload.name) continue;
    pathfinding.data.RemoveGraph(graph);
    return;
}

Functionally this works like a charm, however DeserializeGraphsAdditive always creates a new entry in the graphs array, while RemoveGraph only nulls and does not remove it from the array.
This causes the graph array to get bigger with every level load and I’m currently trying to find the best solution for that.

The most straight forward solution for me would be to make DeserializeGraphsAdditive also check for empty array indexes. That would be consistent with how AddGraph does it. But of course the might be good reasons for why it is not doing that.

Alternatively I could rebuild the graphs array myself after adding/removing an entry, for example by creating a new array without empty entries and then replacing it with the graph-array.
However this would cause all graph indexes to potentially be reordered and I’m not sure what the implications of that would be.
Would love to get another opinion on this :slight_smile:

Cheers,
Leo