Skip to content

Commit 3a09971

Browse files
fix: NetworkVariable change prior to ownership change using distributed authority topology fails to sync NetworkVariable (#4107)
* 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. * fix - update Removing the additional authority check as it is not needed. Fixing a very edge case scenario that shouldn't happen but in the event it does when finding objects by type within a specific scene, if the scene is invalid then use an empty array for the found objects.
1 parent b8a8ddb commit 3a09971

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ private struct ObjectsInSceneEnumerator<T> : IEnumerable<T>, IEnumerator<T> wher
6969
internal ObjectsInSceneEnumerator(Scene scene, bool includeInactive)
7070
{
7171
m_IncludeInactive = includeInactive;
72-
73-
m_RootObjects = scene.GetRootGameObjects();
72+
// If the scene is invalid, use an empty array.
73+
m_RootObjects = scene.IsValid() ? scene.GetRootGameObjects() : new UnityEngine.GameObject[0];
7474
m_RootIndex = 0;
7575
m_CurrentChildObjects = null;
7676
m_CurrentChildIndex = 0;

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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 (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.

0 commit comments

Comments
 (0)