From 5b62fe7dbb24f7e6913e79959ef9a2515b7998ad Mon Sep 17 00:00:00 2001 From: NoelStephensUnity Date: Thu, 21 Nov 2024 17:40:00 -0600 Subject: [PATCH] update A better way to handle object redistribution and NetworkObject synchronization when scene management is disabled, --- .../Runtime/Core/NetworkBehaviourUpdater.cs | 3 ++ .../Runtime/Core/NetworkManager.cs | 38 ++++++++++++------- .../Messages/ChangeOwnershipMessage.cs | 4 +- .../Messages/ClientConnectedMessage.cs | 9 +++-- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviourUpdater.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviourUpdater.cs index 9062ebf113..6a1dd748e6 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviourUpdater.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviourUpdater.cs @@ -141,6 +141,9 @@ private void NetworkBehaviourUpdater_Tick() // Then show any NetworkObjects queued to be made visible/shown m_NetworkManager.SpawnManager.HandleNetworkObjectShow(); + + // Handle object redistribution (DA + disabled scene management only) + m_NetworkManager.HandleRedistributionToClients(); } } } diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs index 56a82bc33f..26be7c9266 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs @@ -162,10 +162,30 @@ public bool DAHost } } - // DANGO-TODO-MVP: Remove these properties once the service handles object distribution - internal ulong ClientToRedistribute; - internal bool RedistributeToClient; - internal int TickToRedistribute; + // DANGO-TODO: Determine if this needs to be removed once the service handles object distribution + internal List ClientsToRedistribute = new List(); + internal bool RedistributeToClients; + + /// + /// Handles object redistribution when scene management is disabled. + /// + /// DANGO-TODO: Determine if this needs to be removed once the service handles object distribution + /// + internal void HandleRedistributionToClients() + { + if (!DistributedAuthorityMode || !RedistributeToClients || NetworkConfig.EnableSceneManagement || ShutdownInProgress) + { + return; + } + + foreach (var clientId in ClientsToRedistribute) + { + SpawnManager.DistributeNetworkObjects(clientId); + } + RedistributeToClients = false; + ClientsToRedistribute.Clear(); + } + internal List DeferredDespawnObjects = new List(); @@ -393,16 +413,6 @@ public void NetworkUpdate(NetworkUpdateStage updateStage) // This is "ok" to invoke when not processing messages since it is just cleaning up messages that never got handled within their timeout period. DeferredMessageManager.CleanupStaleTriggers(); - // DANGO-TODO-MVP: Remove this once the service handles object distribution - // NOTE: This needs to be the last thing done and should happen exactly at this point - // in the update - if (RedistributeToClient && ServerTime.Tick <= TickToRedistribute) - { - RedistributeToClient = false; - SpawnManager.DistributeNetworkObjects(ClientToRedistribute); - ClientToRedistribute = 0; - } - if (m_ShuttingDown) { // Host-server will disconnect any connected clients prior to finalizing its shutdown diff --git a/com.unity.netcode.gameobjects/Runtime/Messaging/Messages/ChangeOwnershipMessage.cs b/com.unity.netcode.gameobjects/Runtime/Messaging/Messages/ChangeOwnershipMessage.cs index 35071c4940..cb4f114b91 100644 --- a/com.unity.netcode.gameobjects/Runtime/Messaging/Messages/ChangeOwnershipMessage.cs +++ b/com.unity.netcode.gameobjects/Runtime/Messaging/Messages/ChangeOwnershipMessage.cs @@ -332,8 +332,8 @@ private void HandleOwnershipChange(ref NetworkContext context) // Sanity check that we are not sending duplicated change ownership messages if (networkObject.OwnerClientId == OwnerClientId) { - UnityEngine.Debug.LogError($"Client-{context.SenderId} sent unnecessary ownership changed message for {NetworkObjectId}."); - // Ignore the message + // Log error and then ignore the message + UnityEngine.Debug.LogError($"Client-{context.SenderId} ({RequestClientId}) sent unnecessary ownership changed message for {NetworkObjectId}."); return; } diff --git a/com.unity.netcode.gameobjects/Runtime/Messaging/Messages/ClientConnectedMessage.cs b/com.unity.netcode.gameobjects/Runtime/Messaging/Messages/ClientConnectedMessage.cs index f86c3c6cca..0234bc8b67 100644 --- a/com.unity.netcode.gameobjects/Runtime/Messaging/Messages/ClientConnectedMessage.cs +++ b/com.unity.netcode.gameobjects/Runtime/Messaging/Messages/ClientConnectedMessage.cs @@ -68,10 +68,11 @@ public void Handle(ref NetworkContext context) // networkManager.SpawnManager.ShowHiddenObjectsToNewlyJoinedClient(ClientId); //} - // We defer redistribution to the end of the NetworkUpdateStage.PostLateUpdate - networkManager.RedistributeToClient = true; - networkManager.ClientToRedistribute = ClientId; - networkManager.TickToRedistribute = networkManager.ServerTime.Tick + (int)(0.5f * networkManager.NetworkConfig.TickRate); + /// We defer redistribution to happen after NetworkShow has been invoked + /// + /// DANGO-TODO: Determine if this needs to be removed once the service handles object distribution + networkManager.RedistributeToClients = true; + networkManager.ClientsToRedistribute.Add(ClientId); } } }