Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 14 additions & 20 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,14 +395,13 @@ private void CheckForInScenePlaced()
/// <param name="destroy">Defaults to true, determines whether the <see cref="NetworkObject"/> will be destroyed.</param>
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;
}

Expand All @@ -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)
{
Expand Down Expand Up @@ -612,7 +610,7 @@ internal void RemoveOwnershipExtended(OwnershipStatusExtended extended)
/// <returns>true or false depending upon lock operation's success</returns>
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)
Expand All @@ -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)
{
Expand Down Expand Up @@ -671,7 +668,6 @@ public bool SetOwnershipLock(bool lockOwnership = true)
{
SendOwnershipStatusUpdate();
}

return true;
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -866,7 +861,7 @@ public OwnershipRequestStatus RequestOwnership()
public OnOwnershipRequestedDelegateHandler OnOwnershipRequested;

/// <summary>
/// Invoked by ChangeOwnershipMessage
/// Invoked by <see cref="ChangeOwnershipMessage"/>
/// </summary>
/// <param name="clientRequestingOwnership">the client requesting ownership</param>
internal void OwnershipRequest(ulong clientRequestingOwnership)
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -1506,7 +1501,7 @@ public void NetworkShow(ulong clientId)
return;
}

if (!HasAuthority)
if (!InternalHasAuthority())
{
if (NetworkManagerOwner.DistributedAuthorityMode)
{
Expand Down Expand Up @@ -1601,7 +1596,7 @@ public void NetworkHide(ulong clientId)
return;
}

if (!HasAuthority)
if (!InternalHasAuthority())
{
if (NetworkManagerOwner.DistributedAuthorityMode)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -2099,7 +2094,7 @@ public void ChangeOwnership(ulong newOwnerClientId)
}
return;
}
NetworkManagerOwner.SpawnManager.ChangeOwnership(this, newOwnerClientId, HasAuthority);
NetworkManagerOwner.SpawnManager.ChangeOwnership(this, newOwnerClientId, InternalHasAuthority());
}

/// <summary>
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -3404,7 +3399,6 @@ internal static NetworkObject Deserialize(in SerializedObject serializedObject,
{
Destroy(networkObject.gameObject);
}

return null;
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down