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

Rising this post from the grave as I encounter the same problem in a similar situation.

RemoveGraph’s description mention that the nulled entries will be used when adding new graphs with DeserializeGraphsAdditive, however that does not seem to be the case as DeserializeGraphsPartAdditive seems to remove only the last entries of the list if they’re null.

Just as Leoo, Im hesitant to clean the list myself because of potential indexing issues.

Any fixes planned on this, or is this an user misunderstanding?

PS: Actually the indexing is recomputed just after the clipping of the null entries. It seems that replacing

> while (gr.Count > 0 && gr[gr.Count-1] == null) gr.RemoveAt(gr.Count-1);

*by *

> gr.RemoveAll(g => g == null);

should do the trick

PPS: This did not, in fact, do the trick. Index shenanigans indeed.

Hi

Thanks. Good catch.
I’ll include a fix for this in the next update. New graphs will be loaded into the empty slots, regardless of where in the array they are.

Well, that was impressively fast.
Thank you.