Hello,
My game is tile based so that as the player proceeds through the level new tiles spawn. For each tile I have pregenerated a serialised nav mesh and I call AstarData.DeserializeGraphs() on each tile transition to load the new meshes. I need to deserialize up to four graphs each transition (for the tile and surrounding connectors). I was doing this in the main Unity thread but it causes a large frame spike. To avoid this spike I spawned another thread and called DeserializeGraphs() from there. This generally works but occasionally I get the following error and deserialization fails:
Caught exception while deserializing data.
System.Exception: Trying to initialize a node when it is not safe to initialize any nodes. Must be done during a graph update
at AstarPath.InitializeNode (Pathfinding.GraphNode node) [0x00010] in …\AstarPathfindingProject\Core\AstarPath.cs:1929
at Pathfinding.GraphNode…ctor (.AstarPath astar) [0x0001e] in …\Assets\AstarPathfindingProject\Core\Nodes\GraphNode.cs:34
at Pathfinding.MeshNode…ctor (.AstarPath astar) [0x00000] in :0
at Pathfinding.TriangleMeshNode…ctor (.AstarPath astar) [0x00000] in :0
at Pathfinding.RecastGraph.DeserializeExtraInfo (Pathfinding.Serialization.GraphSerializationContext ctx) [0x001f9] in …\Assets\AstarPathfindingProject\Generators\RecastGenerator.cs:2506
at Pathfinding.Serialization.AstarSerializer.DeserializeExtraInfo (Pathfinding.NavGraph graph) [0x0004b] in …\Assets\AstarPathfindingProject\Core\Serialization\JsonSerializer.cs:693
at Pathfinding.Serialization.AstarSerializer.DeserializeExtraInfo () [0x00009] in …\Assets\AstarPathfindingProject\Core\Serialization\JsonSerializer.cs:774
at Pathfinding.AstarData.DeserializeGraphsPartAdditive (Pathfinding.Serialization.AstarSerializer sr) [0x000af] in …\Assets\AstarPathfindingProject\Core\AstarData.cs:372
at Pathfinding.AstarData.DeserializeGraphsAdditive (System.Byte[] bytes) [0x00023] in …\Assets\AstarPathfindingProject\Core\AstarData.cs:311
UnityEngine.Debug:LogWarning(Object)
Pathfinding.AstarData:DeserializeGraphsAdditive(Byte[]) (at Assets/AstarPathfindingProject/Core/AstarData.cs:321)
[ApplicationCode]
It looks as though the AstarPath.pathQueue is populated during the deserialize causing this error even though AstarPath.BlockUntilPathQueueBlocked() is called in DeserializeGraphsAdditive() and DeserializeGraphs(). I tried setting AstarPath.enable to false before the deserialize so Update() isn’t called but this didn’t help.
Any suggestions about how I should approach running the deserialisation in a background thread?
Thanks,
Hamish