Skip to content

Commit 5b62fe7

Browse files
update
A better way to handle object redistribution and NetworkObject synchronization when scene management is disabled,
1 parent 311216b commit 5b62fe7

File tree

4 files changed

+34
-20
lines changed

4 files changed

+34
-20
lines changed

com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviourUpdater.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ private void NetworkBehaviourUpdater_Tick()
141141

142142
// Then show any NetworkObjects queued to be made visible/shown
143143
m_NetworkManager.SpawnManager.HandleNetworkObjectShow();
144+
145+
// Handle object redistribution (DA + disabled scene management only)
146+
m_NetworkManager.HandleRedistributionToClients();
144147
}
145148
}
146149
}

com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,30 @@ public bool DAHost
162162
}
163163
}
164164

165-
// DANGO-TODO-MVP: Remove these properties once the service handles object distribution
166-
internal ulong ClientToRedistribute;
167-
internal bool RedistributeToClient;
168-
internal int TickToRedistribute;
165+
// DANGO-TODO: Determine if this needs to be removed once the service handles object distribution
166+
internal List<ulong> ClientsToRedistribute = new List<ulong>();
167+
internal bool RedistributeToClients;
168+
169+
/// <summary>
170+
/// Handles object redistribution when scene management is disabled.
171+
/// <see cref="NetworkBehaviourUpdater.NetworkBehaviourUpdater_Tick"/>
172+
/// DANGO-TODO: Determine if this needs to be removed once the service handles object distribution
173+
/// </summary>
174+
internal void HandleRedistributionToClients()
175+
{
176+
if (!DistributedAuthorityMode || !RedistributeToClients || NetworkConfig.EnableSceneManagement || ShutdownInProgress)
177+
{
178+
return;
179+
}
180+
181+
foreach (var clientId in ClientsToRedistribute)
182+
{
183+
SpawnManager.DistributeNetworkObjects(clientId);
184+
}
185+
RedistributeToClients = false;
186+
ClientsToRedistribute.Clear();
187+
}
188+
169189

170190
internal List<NetworkObject> DeferredDespawnObjects = new List<NetworkObject>();
171191

@@ -393,16 +413,6 @@ public void NetworkUpdate(NetworkUpdateStage updateStage)
393413
// This is "ok" to invoke when not processing messages since it is just cleaning up messages that never got handled within their timeout period.
394414
DeferredMessageManager.CleanupStaleTriggers();
395415

396-
// DANGO-TODO-MVP: Remove this once the service handles object distribution
397-
// NOTE: This needs to be the last thing done and should happen exactly at this point
398-
// in the update
399-
if (RedistributeToClient && ServerTime.Tick <= TickToRedistribute)
400-
{
401-
RedistributeToClient = false;
402-
SpawnManager.DistributeNetworkObjects(ClientToRedistribute);
403-
ClientToRedistribute = 0;
404-
}
405-
406416
if (m_ShuttingDown)
407417
{
408418
// Host-server will disconnect any connected clients prior to finalizing its shutdown

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,8 @@ private void HandleOwnershipChange(ref NetworkContext context)
332332
// Sanity check that we are not sending duplicated change ownership messages
333333
if (networkObject.OwnerClientId == OwnerClientId)
334334
{
335-
UnityEngine.Debug.LogError($"Client-{context.SenderId} sent unnecessary ownership changed message for {NetworkObjectId}.");
336-
// Ignore the message
335+
// Log error and then ignore the message
336+
UnityEngine.Debug.LogError($"Client-{context.SenderId} ({RequestClientId}) sent unnecessary ownership changed message for {NetworkObjectId}.");
337337
return;
338338
}
339339

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ public void Handle(ref NetworkContext context)
6868
// networkManager.SpawnManager.ShowHiddenObjectsToNewlyJoinedClient(ClientId);
6969
//}
7070

71-
// We defer redistribution to the end of the NetworkUpdateStage.PostLateUpdate
72-
networkManager.RedistributeToClient = true;
73-
networkManager.ClientToRedistribute = ClientId;
74-
networkManager.TickToRedistribute = networkManager.ServerTime.Tick + (int)(0.5f * networkManager.NetworkConfig.TickRate);
71+
/// We defer redistribution to happen after NetworkShow has been invoked
72+
/// <see cref="NetworkBehaviourUpdater.NetworkBehaviourUpdater_Tick"/>
73+
/// DANGO-TODO: Determine if this needs to be removed once the service handles object distribution
74+
networkManager.RedistributeToClients = true;
75+
networkManager.ClientsToRedistribute.Add(ClientId);
7576
}
7677
}
7778
}

0 commit comments

Comments
 (0)