Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
A better way to handle object redistribution and NetworkObject synchronization when scene management is disabled,
  • Loading branch information
NoelStephensUnity committed Nov 21, 2024
1 parent 311216b commit 5b62fe7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
38 changes: 24 additions & 14 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ulong> ClientsToRedistribute = new List<ulong>();
internal bool RedistributeToClients;

/// <summary>
/// Handles object redistribution when scene management is disabled.
/// <see cref="NetworkBehaviourUpdater.NetworkBehaviourUpdater_Tick"/>
/// DANGO-TODO: Determine if this needs to be removed once the service handles object distribution
/// </summary>
internal void HandleRedistributionToClients()
{
if (!DistributedAuthorityMode || !RedistributeToClients || NetworkConfig.EnableSceneManagement || ShutdownInProgress)
{
return;
}

foreach (var clientId in ClientsToRedistribute)
{
SpawnManager.DistributeNetworkObjects(clientId);
}
RedistributeToClients = false;
ClientsToRedistribute.Clear();
}


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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
/// <see cref="NetworkBehaviourUpdater.NetworkBehaviourUpdater_Tick"/>
/// DANGO-TODO: Determine if this needs to be removed once the service handles object distribution
networkManager.RedistributeToClients = true;
networkManager.ClientsToRedistribute.Add(ClientId);
}
}
}
Expand Down

0 comments on commit 5b62fe7

Please sign in to comment.