Skip to content

Commit 526f30b

Browse files
update
Possible fix for an edge case scenario during synchronization.
1 parent 9038909 commit 526f30b

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

com.unity.netcode.gameobjects/Runtime/SceneManagement/SceneEventData.cs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,24 +1146,37 @@ internal void SynchronizeSceneNetworkObjects(NetworkManager networkManager)
11461146

11471147
#if UNIFIED_NETCODE
11481148
// TODO-UNIFIED: This is a temporary POC fix to handle synchronizing hybrid spawned objects where the Ghost instance might not yet exist.
1149-
if (serializedObject.HasGhost && !networkManager.SpawnManager.GhostsPendingSpawn.ContainsKey(serializedObject.NetworkObjectId))
1149+
if (serializedObject.HasGhost)
11501150
{
1151-
if (networkManager.LogLevel == LogLevel.Developer)
1151+
if (!networkManager.SpawnManager.GhostsPendingSpawn.ContainsKey(serializedObject.NetworkObjectId))
11521152
{
1153-
UnityEngine.Debug.Log($"[{nameof(SceneEventData)}][{nameof(SynchronizeSceneNetworkObjects)}] Deferring creation of NetworkObjectId-{serializedObject.NetworkObjectId} to wait for Ghost.");
1154-
}
1155-
1156-
var newEntry = new PendingGhostSpawnEntry()
1157-
{
1158-
RegistrationTime = UnityEngine.Time.realtimeSinceStartup,
1159-
SerializedObject = serializedObject,
1160-
Buffer = new FastBufferReader(InternalBuffer, Allocator.Persistent, serializedObject.SynchronizationDataSize, InternalBuffer.Position)
1161-
};
1153+
if (networkManager.LogLevel == LogLevel.Developer)
1154+
{
1155+
UnityEngine.Debug.Log($"[{nameof(SceneEventData)}][{nameof(SynchronizeSceneNetworkObjects)}] Deferring creation of NetworkObjectId-{serializedObject.NetworkObjectId} to wait for Ghost.");
1156+
}
11621157

1163-
spawnManager.RegisterGhostPendingSynchronization(newEntry);
1158+
var newEntry = new PendingGhostSpawnEntry()
1159+
{
1160+
RegistrationTime = UnityEngine.Time.realtimeSinceStartup,
1161+
SerializedObject = serializedObject,
1162+
Buffer = new FastBufferReader(InternalBuffer, Allocator.Persistent, serializedObject.SynchronizationDataSize, InternalBuffer.Position)
1163+
};
11641164

1165-
InternalBuffer.Seek(InternalBuffer.Position + serializedObject.SynchronizationDataSize);
1166-
continue;
1165+
spawnManager.RegisterGhostPendingSynchronization(newEntry);
1166+
InternalBuffer.Seek(InternalBuffer.Position + serializedObject.SynchronizationDataSize);
1167+
continue;
1168+
}
1169+
else if (networkManager.SpawnManager.GhostsPendingSpawn[serializedObject.NetworkObjectId] == null)
1170+
{
1171+
if (networkManager.LogLevel == LogLevel.Developer)
1172+
{
1173+
UnityEngine.Debug.Log($"[{nameof(SceneEventData)}][{nameof(SynchronizeSceneNetworkObjects)}] Dropping creation of NetworkObjectId-{serializedObject.NetworkObjectId} as it has an entry but no longer exists!");
1174+
}
1175+
// If it no longer exists, then just remove the entry and skip it.
1176+
InternalBuffer.Seek(InternalBuffer.Position + serializedObject.SynchronizationDataSize);
1177+
networkManager.SpawnManager.GhostsPendingSpawn.Remove(serializedObject.NetworkObjectId);
1178+
continue;
1179+
}
11671180
}
11681181
#endif
11691182

0 commit comments

Comments
 (0)