Skip to content

Commit 9571eda

Browse files
fix
This resolves the issue where in a distributed authority session changing a NetworkVariable prior to changing ownership in the same call-stack would result in the NetworkVariable not being synchronized.
1 parent b998fc0 commit 9571eda

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2961,7 +2961,7 @@ internal bool InitializeChildNetworkBehaviours()
29612961
/// </remarks>
29622962
/// <param name="originalOwnerId">the owner prior to beginning the change in ownership change.</param>
29632963
/// <param name="originalPreviousOwnerId">the previous owner prior to beginning the change in ownership change.</param>
2964-
internal void SynchronizeOwnerNetworkVariables(ulong originalOwnerId, ulong originalPreviousOwnerId)
2964+
internal void SynchronizeOwnerNetworkVariables(ulong originalOwnerId, ulong originalPreviousOwnerId, bool authorityChangedOwnership = false)
29652965
{
29662966
var currentOwnerId = OwnerClientId;
29672967
OwnerClientId = originalOwnerId;
@@ -2971,17 +2971,31 @@ internal void SynchronizeOwnerNetworkVariables(ulong originalOwnerId, ulong orig
29712971
childBehaviour.MarkOwnerReadDirtyAndCheckOwnerWriteIsDirty();
29722972
}
29732973

2974+
// If the spawn authority of a distributed authority network topology has invoked a change in ownership,
2975+
// then we want to invoke the NetworkBehaviourUpdate prior to changing the owner back.
2976+
if (authorityChangedOwnership && NetworkManager.DistributedAuthorityMode)
2977+
{
2978+
// Force send a state update for all owner read NetworkVariables and any currently dirty
2979+
// owner write NetworkVariables.
2980+
NetworkManagerOwner.BehaviourUpdater.NetworkBehaviourUpdate(true);
2981+
}
2982+
29742983
// Now set the new owner and previous owner identifiers back to their original new values
2975-
// before we run the NetworkBehaviourUpdate. For owner read only permissions this order of
2976-
// operations is **particularly important** as we need to first (above) mark things as dirty
2977-
// from the context of the original owner and then second (below) we need to send the messages
2978-
// which requires the new owner to be set for owner read permission NetworkVariables.
2984+
// after we run the NetworkBehaviourUpdate.
29792985
OwnerClientId = currentOwnerId;
29802986
PreviousOwnerId = originalOwnerId;
29812987

2982-
// Force send a state update for all owner read NetworkVariables and any currently dirty
2983-
// owner write NetworkVariables.
2984-
NetworkManagerOwner.BehaviourUpdater.NetworkBehaviourUpdate(true);
2988+
// For owner read only permissions this order of operations is **particularly important** as
2989+
// we need to first (above) mark things as dirty from the context of the original owner and
2990+
// then second (below) we need to send the messages which requires the new owner to be set.
2991+
// Note: Owner read only NetworkVariables do not make sense in a distributed authority topology
2992+
// since the only instance that can write is the owner.
2993+
if (!NetworkManager.DistributedAuthorityMode)
2994+
{
2995+
// Force send a state update for all owner read NetworkVariables and any currently dirty
2996+
// owner write NetworkVariables.
2997+
NetworkManagerOwner.BehaviourUpdater.NetworkBehaviourUpdate(true);
2998+
}
29852999
}
29863000

29873001
// NGO currently guarantees that the client will receive spawn data for all objects in one network tick.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ private void HandleOwnershipChange(ref NetworkContext context, ref NetworkManage
241241
if (!distributedAuthorityMode && originalOwner == networkManager.LocalClientId)
242242
{
243243
// Fully synchronize NetworkVariables with either read or write ownership permissions.
244-
networkObject.SynchronizeOwnerNetworkVariables(originalOwner, networkObject.PreviousOwnerId);
244+
networkObject.SynchronizeOwnerNetworkVariables(originalOwner, networkObject.PreviousOwnerId, true);
245245
}
246246

247247
// Always invoke ownership change notifications

0 commit comments

Comments
 (0)