Skip to content

Commit 9eac4dd

Browse files
style
Adding some additional comments in various places to help clarify a bit more what is happening.
1 parent 6494ff4 commit 9eac4dd

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

com.unity.netcode.gameobjects/Runtime/Messaging/Messages/SceneEventMessage.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int
2121
{
2222
var networkManager = (NetworkManager)context.SystemOwner;
2323
#if UNIFIED_NETCODE
24+
// Defer this message if the OnGhostSpawned trigger is still being processed. This is because the scene event message can be sent
25+
// as part of the ghost spawning process and we want to make sure that all ghost spawning related messages are processed before we
26+
// process this one. This is to avoid any potential issues with the order of message processing and to ensure that all ghost
27+
// related messages are processed before we process this one.
2428
if (networkManager.DeferredMessageManager.HasAnyOfTrigger(IDeferredNetworkMessageManager.TriggerType
2529
.OnGhostSpawned))
2630
{

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,6 @@ internal void SynchronizeSceneNetworkObjects(NetworkManager networkManager)
11081108
builder.AppendLine($"[Read][Synchronize Objects][WPos: {InternalBuffer.Position}][NO-Count: {newObjectsCount}] Begin:");
11091109
}
11101110
#if UNIFIED_NETCODE
1111-
// TODO-UNIFIED: This is a temporary POC fix to handle hybrid spawning where the Ghost instance might not yet exist.
11121111
var spawnManager = m_NetworkManager.SpawnManager;
11131112
#endif
11141113

@@ -1119,7 +1118,10 @@ internal void SynchronizeSceneNetworkObjects(NetworkManager networkManager)
11191118
serializedObject.Deserialize(InternalBuffer);
11201119

11211120
#if UNIFIED_NETCODE
1122-
// TODO-UNIFIED: This is a temporary POC fix to handle synchronizing hybrid spawned objects where the Ghost instance might not yet exist.
1121+
// This handles the case where a NetworkObject is serialized with a ghost component but the ghost isn't actually included in
1122+
// the spawn message and won't be spawned by the client until later in the N4E synchronization process. In this case, we need
1123+
// to defer the deserialization of the NetworkObject until the ghost is spawned and we have an instance to deserialize this
1124+
// information during synchronization.
11231125
if (serializedObject.HasGhost)
11241126
{
11251127
if (!networkManager.SpawnManager.GhostsPendingSpawn.ContainsKey(serializedObject.NetworkObjectId))
@@ -1442,12 +1444,18 @@ internal SceneEventData(NetworkManager networkManager)
14421444
}
14431445

14441446
#if UNIFIED_NETCODE
1447+
/// <summary>
1448+
/// Used to store pending ghost spawns that are waiting for their associated (N4E) ghost to be spawned before they can be fully deserialized and
1449+
/// spawned during the scene synchronization process. This is necessary because in unified mode we allow for NetworkObjects with ghost components
1450+
/// to be synchronized during the scene synchronization process but we can't guarantee the order of messages that the client receives so we
1451+
/// need to defer the deserialization of any NetworkObject that has a ghost component until we have received the message that the ghost has
1452+
/// been spawned and we have an instance to deserialize this information into.
1453+
/// </summary>
14451454
internal struct PendingGhostSpawnEntry
14461455
{
14471456
public float RegistrationTime;
14481457
public FastBufferReader Buffer;
14491458
public NetworkObject.SerializedObject SerializedObject;
1450-
14511459
}
14521460
#endif
14531461
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,9 @@ internal bool AuthorityLocalSpawn([NotNull] NetworkObject networkObject, ulong n
13681368
}
13691369

13701370
#if UNIFIED_NETCODE
1371+
// If this is a hybrid prefab, the spawn authority is responsible for assigning the network object id to the network object bridge so that it
1372+
// can be used to link the N4E ghost to the NetworkObject. This is needed because in the hybrid prefab case, the ghost can be spawned before
1373+
// the NetworkObject is fully spawned.
13711374
if (networkObject.HasGhost)
13721375
{
13731376
networkObject.NetworkObjectBridge.SetNetworkObjectId(networkObject.NetworkObjectId);
@@ -2100,7 +2103,7 @@ internal void OnDespawnObject([NotNull] NetworkObject networkObject, bool destro
21002103
RemovePlayerObject(networkObject, destroyGameObject);
21012104
}
21022105
#if UNIFIED_NETCODE
2103-
// Let unified netcode handle destroying
2106+
// Unified netcode handles destroying the instance of the object on the non-authority side when the object has a ghost representation.
21042107
if (destroyGameObject && networkObject.HasGhost && !NetworkManager.IsServer)
21052108
{
21062109
// exit early

0 commit comments

Comments
 (0)