diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs index 73cc6c7e2d..744b142d96 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs @@ -395,14 +395,13 @@ private void CheckForInScenePlaced() /// Defaults to true, determines whether the will be destroyed. public void DeferDespawn(int tickOffset, bool destroy = true) { - // Ensure we log the DAMode message first as locking ownership is not allowed if not DA so the DA message is the most relevant. + // The DAMode message is logged first, as ownership locking isn’t allowed when not in DAMode, making it the most relevant message. if (!NetworkManager.DistributedAuthorityMode) { if (NetworkManager.LogLevel <= LogLevel.Error) { NetworkLog.LogErrorServer($"[{name}] This method is only available in distributed authority mode."); } - return; } @@ -412,11 +411,10 @@ public void DeferDespawn(int tickOffset, bool destroy = true) { NetworkLog.LogErrorServer($"[{name}] Cannot defer despawn while not spawned."); } - return; } - if (!HasAuthority) + if (!InternalHasAuthority()) { if (NetworkManagerOwner.LogLevel <= LogLevel.Error) { @@ -612,7 +610,7 @@ internal void RemoveOwnershipExtended(OwnershipStatusExtended extended) /// true or false depending upon lock operation's success public bool SetOwnershipLock(bool lockOwnership = true) { - // Ensure we log the DAMode message first as locking ownership is not allowed if not DA so the DA message is the most relevant. + // The DAMode message is logged first, as ownership locking isn’t allowed when not in DAMode, making it the most relevant message. if (!NetworkManager.DistributedAuthorityMode) { if (NetworkManager.LogLevel <= LogLevel.Error) @@ -628,12 +626,11 @@ public bool SetOwnershipLock(bool lockOwnership = true) { NetworkLog.LogErrorServer($"[{name}][Attempted Lock While not spawned]"); } - return false; } // If we don't have authority exit early - if (!HasAuthority) + if (!InternalHasAuthority()) { if (NetworkManager.LogLevel <= LogLevel.Error) { @@ -671,7 +668,6 @@ public bool SetOwnershipLock(bool lockOwnership = true) { SendOwnershipStatusUpdate(); } - return true; } @@ -797,7 +793,6 @@ public OwnershipRequestStatus RequestOwnership() { NetworkLog.LogErrorServer($"[{name}][Invalid Operation] Cannot request ownership of an NetworkObject before it is spawned."); } - return OwnershipRequestStatus.InvalidOperation; } // Exit early the local client is already the owner @@ -866,7 +861,7 @@ public OwnershipRequestStatus RequestOwnership() public OnOwnershipRequestedDelegateHandler OnOwnershipRequested; /// - /// Invoked by ChangeOwnershipMessage + /// Invoked by /// /// the client requesting ownership internal void OwnershipRequest(ulong clientRequestingOwnership) @@ -909,7 +904,7 @@ internal void OwnershipRequest(ulong clientRequestingOwnership) // This action is always authorized as long as the client still has authority. // We need to pass in that this is a request approval ownership change. - NetworkManagerOwner.SpawnManager.ChangeOwnership(this, clientRequestingOwnership, HasAuthority, true); + NetworkManagerOwner.SpawnManager.ChangeOwnership(this, clientRequestingOwnership, InternalHasAuthority(), true); } else { @@ -1506,7 +1501,7 @@ public void NetworkShow(ulong clientId) return; } - if (!HasAuthority) + if (!InternalHasAuthority()) { if (NetworkManagerOwner.DistributedAuthorityMode) { @@ -1601,7 +1596,7 @@ public void NetworkHide(ulong clientId) return; } - if (!HasAuthority) + if (!InternalHasAuthority()) { if (NetworkManagerOwner.DistributedAuthorityMode) { @@ -1760,7 +1755,7 @@ private void OnDestroy() { // An authorized destroy is when done by the authority instance or done due to a scene event and the NetworkObject // was marked as destroy pending scene event (which means the destroy with scene property was set). - var isAuthorityDestroy = HasAuthority || NetworkManager.DAHost || DestroyPendingSceneEvent; + var isAuthorityDestroy = InternalHasAuthority() || NetworkManager.DAHost || DestroyPendingSceneEvent; // If the NetworkObject's GameObject is still valid and the scene is still valid and loaded, then we are still valid var isStillValid = gameObject != null && gameObject.scene.IsValid() && gameObject.scene.isLoaded; @@ -2099,7 +2094,7 @@ public void ChangeOwnership(ulong newOwnerClientId) } return; } - NetworkManagerOwner.SpawnManager.ChangeOwnership(this, newOwnerClientId, HasAuthority); + NetworkManagerOwner.SpawnManager.ChangeOwnership(this, newOwnerClientId, InternalHasAuthority()); } /// @@ -2334,7 +2329,7 @@ public bool TrySetParent(NetworkObject parent, bool worldPositionStays = true) // DANGO-TODO: Do we want to worry about ownership permissions here? // It wouldn't make sense to not allow parenting, but keeping this note here as a reminder. - var isAuthority = HasAuthority || (AllowOwnerToParent && IsOwner); + var isAuthority = InternalHasAuthority() || (AllowOwnerToParent && IsOwner); // If we don't have authority and we are not shutting down, then don't allow any parenting. // If we are shutting down and don't have authority then allow it. @@ -2409,7 +2404,7 @@ private void OnTransformParentChanged() // With distributed authority, we need to track "valid authoritative" parenting changes. // So, either the authority or AuthorityAppliedParenting is considered a "valid parenting change". - var isParentingAuthority = HasAuthority || AuthorityAppliedParenting || (AllowOwnerToParent && IsOwner); + var isParentingAuthority = InternalHasAuthority() || AuthorityAppliedParenting || (AllowOwnerToParent && IsOwner); // If we are spawned and don't have authority; reset the parent back to the cached parent and exit if (!isParentingAuthority) { @@ -3404,7 +3399,6 @@ internal static NetworkObject Deserialize(in SerializedObject serializedObject, { Destroy(networkObject.gameObject); } - return null; } @@ -3526,7 +3520,7 @@ internal void SceneChangedUpdate(Scene scene, bool notify = false) return; } - var isAuthority = HasAuthority; + var isAuthority = InternalHasAuthority(); SceneOriginHandle = scene.handle; // non-authority needs to update the NetworkSceneHandle @@ -3561,7 +3555,7 @@ internal void SceneChangedUpdate(Scene scene, bool notify = false) OnMigratedToNewScene?.Invoke(); // Only the authority side will notify clients of non-parented NetworkObject scene changes - if (isAuthority && notify && !transform.parent) + if (isAuthority && notify && transform.parent == null) { NetworkManagerOwner.SceneManager.NotifyNetworkObjectSceneChanged(this); }