Skip to content

Commit fe4cff9

Browse files
update
Moved `PendingGhostSpawnEntry` into the GhostSpawnManager.cs file. Made `PendingGhostSpawnEntry` implement `IDisposable` for simplification and best practices purposes.
1 parent 29e08c6 commit fe4cff9

2 files changed

Lines changed: 30 additions & 19 deletions

File tree

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,6 @@ private void DeserializeObjectsMovedIntoNewScene(FastBufferReader reader)
13091309
}
13101310
}
13111311

1312-
13131312
/// <summary>
13141313
/// While a client is synchronizing ObjectSceneChanged messages could be received.
13151314
/// This defers any ObjectSceneChanged message processing to occur after the client
@@ -1414,20 +1413,4 @@ internal SceneEventData(NetworkManager networkManager)
14141413
SceneEventId = XXHash.Hash32(Guid.NewGuid().ToString());
14151414
}
14161415
}
1417-
1418-
#if UNIFIED_NETCODE
1419-
/// <summary>
1420-
/// Used to store pending ghost spawns that are waiting for their associated (N4E) ghost to be spawned before they can be fully deserialized and
1421-
/// spawned during the scene synchronization process. This is necessary because in unified mode we allow for NetworkObjects with ghost components
1422-
/// to be synchronized during the scene synchronization process but we can't guarantee the order of messages that the client receives so we
1423-
/// need to defer the deserialization of any NetworkObject that has a ghost component until we have received the message that the ghost has
1424-
/// been spawned and we have an instance to deserialize this information into.
1425-
/// </summary>
1426-
internal struct PendingGhostSpawnEntry
1427-
{
1428-
public float RegistrationTime;
1429-
public FastBufferReader Buffer;
1430-
public NetworkObject.SerializedObject SerializedObject;
1431-
}
1432-
#endif
14331416
}

com.unity.netcode.gameobjects/Runtime/Spawning/GhostSpawnManager.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
#if UNIFIED_NETCODE
2+
using System;
23
using System.Collections.Generic;
34
using System.Diagnostics.CodeAnalysis;
45
using Unity.Collections;
56
using Unity.Netcode.Logging;
67
using UnityEngine;
8+
using Object = UnityEngine.Object;
79

810
namespace Unity.Netcode
911
{
12+
/// <summary>
13+
/// Handles the management of ghost spawns during the synchronization process. This includes tracking pending NetworkObject spawns
14+
/// that are waiting for their associated ghost to be spawned before they can be fully deserialized.
15+
/// </summary>
1016
internal class GhostSpawnManager
1117
{
1218
private readonly NetworkManager m_NetworkManager;
@@ -139,16 +145,19 @@ internal NetworkObject ProcessGhostPendingSynchronization(ulong networkObjectId,
139145
m_NetworkManager.SceneManager.SetTheSceneBeingSynchronized(serializedObject.NetworkSceneHandle);
140146
}
141147
var networkObject = NetworkObject.Deserialize(serializedObject, reader, m_NetworkManager);
148+
142149
// TODO-UNIFIED: How do we handle the "all in-scene placed objects are spawned notification"?
143150
//if (serializedObject.IsSceneObject)
144151
//{
145152
// networkObject.InternalInSceneNetworkObjectsSpawned();
146153
//}
154+
155+
// If removing, determine if we have any pending ghosts remaining and dispose of this ghost's pending synchronization buffer.
156+
// If not removing, then we will keep the buffer around until we do remove it (either via spawn or timeout).
147157
if (removeUponSpawn)
148158
{
149-
m_GhostsPendingSynchronization.Remove(networkObjectId);
150159
m_GhostsArePendingSynchronization = m_GhostsPendingSynchronization.Count > 0;
151-
ghostPendingSync.Buffer.Dispose();
160+
ghostPendingSync.Dispose();
152161
}
153162
return networkObject;
154163
}
@@ -255,5 +264,24 @@ internal void Shutdown()
255264
m_GhostsPendingSynchronization.Clear();
256265
}
257266
}
267+
268+
/// <summary>
269+
/// Used to store pending ghost spawns that are waiting for their associated (N4E) ghost to be spawned before they can be fully deserialized and
270+
/// spawned during the scene synchronization process. This is necessary because in unified mode we allow for NetworkObjects with ghost components
271+
/// to be synchronized during the scene synchronization process but we can't guarantee the order of messages that the client receives so we
272+
/// need to defer the deserialization of any NetworkObject that has a ghost component until we have received the message that the ghost has
273+
/// been spawned and we have an instance to deserialize this information into.
274+
/// </summary>
275+
internal struct PendingGhostSpawnEntry : IDisposable
276+
{
277+
public float RegistrationTime;
278+
public FastBufferReader Buffer;
279+
public NetworkObject.SerializedObject SerializedObject;
280+
281+
public void Dispose()
282+
{
283+
Buffer.Dispose();
284+
}
285+
}
258286
}
259287
#endif

0 commit comments

Comments
 (0)