AstarData.DeserializeGraphs in worker thread

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

I solved this by creating an AstarWorkItem and calling AstarPath.AddWorkItem(). In the AstarWorkItem.init function I spawned the worker thread and in the the AstarWorkItem.update function I returned true when the work was complete (the init/thread delegate updated a status variable shared by the AstarWorkItem.update delegate). I had to turn AstarWorkItem into a class to get this to work because init was being called every frame as a struct.

Hi

That sounds like the solution I would recommend.
Yeah, init was called every frame previously, I think this is fixed in the beta (or it might still just be fixed in my dev version).

Thanks for the project. It’s been great for us :slight_smile:

Awesome!
Btw, if you bought it using the Unity Asset Store (if you have the pro version, that is), a rating and/or a review is always greatly appreciated and helps support the further development of this package :slight_smile: