Skip to content

Commit 6172c88

Browse files
fix
This resolves the NetworkShow, ChangeOwnership, and then the additional ChangeOwnershipMessage issue for host, server, and DAHost. For the CMB service, we need to adjust ChangeOwnershipMessage to contain the list of target client identifiers the message should be sent to.
1 parent 05f2e3d commit 6172c88

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ private void HandleOwnershipChange(ref NetworkContext context)
333333
if (networkObject.OwnerClientId == OwnerClientId)
334334
{
335335
// Log error and then ignore the message
336-
UnityEngine.Debug.LogError($"Client-{context.SenderId} ({RequestClientId}) sent unnecessary ownership changed message for {NetworkObjectId}.");
336+
NetworkLog.LogError($"[Receiver: Client-{networkManager.LocalClientId}][Sender: Client-{context.SenderId}][RID: {RequestClientId}] Detected unnecessary ownership changed message for {networkObject.name} (NID:{NetworkObjectId}).");
337337
return;
338338
}
339339

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,19 @@ internal void ChangeOwnership(NetworkObject networkObject, ulong clientId, bool
565565
}
566566

567567
var size = 0;
568+
var clientsToShow = new List<ulong>();
569+
if (ClientsToShowObject.ContainsKey(networkObject))
570+
{
571+
clientsToShow = ClientsToShowObject[networkObject];
572+
}
573+
if (ObjectsToShowToClient.ContainsKey(clientId))
574+
{
575+
if (ObjectsToShowToClient[clientId].Contains(networkObject))
576+
{
577+
clientsToShow.Add(clientId);
578+
}
579+
}
580+
568581
if (NetworkManager.DistributedAuthorityMode)
569582
{
570583
var message = new ChangeOwnershipMessage
@@ -578,24 +591,29 @@ internal void ChangeOwnership(NetworkObject networkObject, ulong clientId, bool
578591
OwnershipFlags = (ushort)networkObject.Ownership,
579592
};
580593
// If we are connected to the CMB service or not the DAHost (i.e. pure DA-Clients only)
594+
581595
if (NetworkManager.CMBServiceConnection || !NetworkManager.DAHost)
582596
{
583597
// Always update the network properties in distributed authority mode for the client gaining ownership
584598
for (int i = 0; i < networkObject.ChildNetworkBehaviours.Count; i++)
585599
{
586600
networkObject.ChildNetworkBehaviours[i].UpdateNetworkProperties();
587601
}
602+
// DANGO-TODO: We have no current way of handling the list of clients that should receive this message.
603+
// --- NGO: Extend ChangeOwnershipMessage to contain a list of client identifiers that
604+
// --- CMB Service: Needs to be adjusted to only forward the message provided in the list of client identifiers.
588605
size = NetworkManager.ConnectionManager.SendMessage(ref message, NetworkDelivery.ReliableSequenced, NetworkManager.ServerClientId);
589606
NetworkManager.NetworkMetrics.TrackOwnershipChangeSent(NetworkManager.LocalClientId, networkObject, size);
590607
}
591608
else // We are the DAHost so broadcast the ownership change
592609
{
593610
foreach (var client in NetworkManager.ConnectedClients)
594611
{
595-
if (client.Value.ClientId == NetworkManager.ServerClientId)
612+
if (client.Value.ClientId == NetworkManager.ServerClientId || IsObjectVisibilityPending(client.Key, ref networkObject))
596613
{
597614
continue;
598615
}
616+
599617
if (networkObject.IsNetworkVisibleTo(client.Value.ClientId))
600618
{
601619
size = NetworkManager.ConnectionManager.SendMessage(ref message, NetworkDelivery.ReliableSequenced, client.Value.ClientId);
@@ -613,8 +631,17 @@ internal void ChangeOwnership(NetworkObject networkObject, ulong clientId, bool
613631
};
614632
foreach (var client in NetworkManager.ConnectedClients)
615633
{
634+
if (client.Value.ClientId == NetworkManager.ServerClientId || IsObjectVisibilityPending(client.Key, ref networkObject))
635+
{
636+
continue;
637+
}
616638
if (networkObject.IsNetworkVisibleTo(client.Value.ClientId))
617639
{
640+
Debug.Log($"[ChangeOwnership] Sending change ownership message to Client-{client.Key}");
641+
if (client.Key != client.Value.ClientId)
642+
{
643+
throw new Exception($"Client key {client.Key} does not match the Client Id {client.Value.ClientId}");
644+
}
618645
size = NetworkManager.ConnectionManager.SendMessage(ref message, NetworkDelivery.ReliableSequenced, client.Value.ClientId);
619646
NetworkManager.NetworkMetrics.TrackOwnershipChangeSent(client.Key, networkObject, size);
620647
}
@@ -639,6 +666,27 @@ internal void ChangeOwnership(NetworkObject networkObject, ulong clientId, bool
639666
}
640667
}
641668

669+
/// <summary>
670+
/// Will determine if a client has been granted visibility for a NetworkObject but
671+
/// the <see cref="CreateObjectMessage"/> has yet to be generated for it. Under this case,
672+
/// the client might not need to be sent a message (i.e. <see cref="ChangeOwnershipMessage")
673+
/// </summary>
674+
/// <param name="clientId">the client to check</param>
675+
/// <param name="networkObject">the <see cref="NetworkObject"/> to check if it is pending show</param>
676+
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
677+
internal bool IsObjectVisibilityPending(ulong clientId, ref NetworkObject networkObject)
678+
{
679+
if (NetworkManager.DistributedAuthorityMode && ClientsToShowObject.ContainsKey(networkObject))
680+
{
681+
return ClientsToShowObject[networkObject].Contains(clientId);
682+
}
683+
else if (ObjectsToShowToClient.ContainsKey(clientId))
684+
{
685+
return ObjectsToShowToClient[clientId].Contains(networkObject);
686+
}
687+
return false;
688+
}
689+
642690
internal bool HasPrefab(NetworkObject.SceneObject sceneObject)
643691
{
644692
if (!NetworkManager.NetworkConfig.EnableSceneManagement || !sceneObject.IsSceneObject)

0 commit comments

Comments
 (0)