Skip to content

Commit 94bc9ff

Browse files
committed
Addressing PR review comments
1 parent 54300e1 commit 94bc9ff

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

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

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ internal bool HasParentNetworkObject(Transform transform)
349349
return true;
350350
}
351351

352-
if (m_CachedParent.parent != null)
352+
if (parent.parent != null)
353353
{
354354
return HasParentNetworkObject(parent);
355355
}
@@ -399,19 +399,19 @@ internal bool HasParentNetworkObject(Transform transform)
399399
/// <param name="destroy">Defaults to true, determines whether the <see cref="NetworkObject"/> will be destroyed.</param>
400400
public void DeferDespawn(int tickOffset, bool destroy = true)
401401
{
402-
if (!IsSpawned)
402+
if (!NetworkManager.DistributedAuthorityMode)
403403
{
404-
if (NetworkManager.LogLevel == LogLevel.Error)
405-
{
406-
NetworkLog.LogErrorServer("[Attempted deferred despawn while un-spawned]");
407-
}
408-
404+
NetworkLog.LogErrorServer("This method is only available in distributed authority mode.");
409405
return;
410406
}
411407

412-
if (!NetworkManagerOwner.DistributedAuthorityMode)
408+
if (!IsSpawned)
413409
{
414-
NetworkLog.LogErrorServer("This method is only available in distributed authority mode.");
410+
if (NetworkManager.LogLevel <= LogLevel.Error)
411+
{
412+
NetworkLog.LogErrorServer($"Cannot defer despawning {name} because it is not spawned!");
413+
}
414+
415415
return;
416416
}
417417

@@ -608,9 +608,9 @@ public bool SetOwnershipLock(bool lockOwnership = true)
608608
{
609609
if (!IsSpawned)
610610
{
611-
if (NetworkManager.LogLevel == LogLevel.Error)
611+
if (NetworkManager.LogLevel <= LogLevel.Error)
612612
{
613-
NetworkLog.LogErrorServer("[Attempted Lock While un-spawned]");
613+
NetworkLog.LogErrorServer("[Attempted Lock While not spawned]");
614614
}
615615

616616
return false;
@@ -1201,7 +1201,7 @@ private bool InternalHasAuthority()
12011201
/// <summary>
12021202
/// Gets whether or not the object is owned by anyone
12031203
/// </summary>
1204-
public bool IsOwnedByServer => OwnerClientId == NetworkManager.ServerClientId;
1204+
public bool IsOwnedByServer => IsSpawned && OwnerClientId == NetworkManager.ServerClientId;
12051205

12061206
/// <summary>
12071207
/// Gets if the object has yet been spawned across the network
@@ -1843,7 +1843,7 @@ internal void SpawnInternal(bool destroyWithScene, ulong ownerClientId, bool pla
18431843
{
18441844
//Ownership |= OwnershipStatus.Distributable;
18451845
// DANGO-TODO: Review over don't destroy with owner being set but DistributeOwnership not being set
1846-
if (NetworkManagerOwner.LogLevel == LogLevel.Developer)
1846+
if (NetworkManagerOwner.LogLevel <= LogLevel.Developer)
18471847
{
18481848
NetworkLog.LogWarning("DANGO-TODO: Review over don't destroy with owner being set but DistributeOwnership not being set. For now, if the NetworkObject does not destroy with the owner it will set ownership to SessionOwner.");
18491849
}
@@ -2000,7 +2000,7 @@ public void Despawn(bool destroy = true)
20002000
{
20012001
if (!IsSpawned)
20022002
{
2003-
if (NetworkManager.LogLevel == LogLevel.Error)
2003+
if (NetworkManager.LogLevel <= LogLevel.Error)
20042004
{
20052005
NetworkLog.LogErrorServer($"[Attempted despawn while un-spawned]");
20062006
}
@@ -2032,7 +2032,7 @@ public void RemoveOwnership()
20322032
{
20332033
if (!IsSpawned)
20342034
{
2035-
if (NetworkManager.LogLevel == LogLevel.Error)
2035+
if (NetworkManager.LogLevel <= LogLevel.Error)
20362036
{
20372037
NetworkLog.LogErrorServer("[Attempted ownership removal while un-spawned]");
20382038
}
@@ -2049,7 +2049,7 @@ public void ChangeOwnership(ulong newOwnerClientId)
20492049
{
20502050
if (!IsSpawned)
20512051
{
2052-
if (NetworkManager.LogLevel == LogLevel.Error)
2052+
if (NetworkManager.LogLevel <= LogLevel.Error)
20532053
{
20542054
NetworkLog.LogErrorServer("[Attempted ownership change while un-spawned]");
20552055
}
@@ -2068,7 +2068,7 @@ internal void InvokeBehaviourOnOwnershipChanged(ulong originalOwnerClientId, ulo
20682068
{
20692069
if (!IsSpawned)
20702070
{
2071-
if (NetworkManager.LogLevel == LogLevel.Error)
2071+
if (NetworkManager.LogLevel <= LogLevel.Error)
20722072
{
20732073
NetworkLog.LogErrorServer($"[Attempted behavior invoke on ownership changed while un-spawned]");
20742074
}
@@ -2366,8 +2366,9 @@ private void OnTransformParentChanged()
23662366

23672367
// With distributed authority, we need to track "valid authoritative" parenting changes.
23682368
// So, either the authority or AuthorityAppliedParenting is considered a "valid parenting change".
2369+
var isParentingAuthority = HasAuthority || AuthorityAppliedParenting || (AllowOwnerToParent && IsOwner);
23692370
// If we do not have authority and we are spawned
2370-
if (!(HasAuthority || AuthorityAppliedParenting || (AllowOwnerToParent && IsOwner)))
2371+
if (!isParentingAuthority)
23712372
{
23722373
transform.parent = m_CachedParent;
23732374
if (networkManager.LogLevel >= LogLevel.Normal)
@@ -3242,7 +3243,7 @@ internal SerializedObject Serialize(ulong targetClientId = NetworkManager.Server
32423243
// Handle Parenting
32433244
if (!AlwaysReplicateAsRoot && obj.HasParent)
32443245
{
3245-
var parentNetworkObject = m_CachedParent.GetComponent<NetworkObject>();
3246+
var parentNetworkObject = transform.parent.GetComponent<NetworkObject>();
32463247

32473248
if (parentNetworkObject)
32483249
{
@@ -3523,7 +3524,7 @@ internal void SceneChangedUpdate(Scene scene, bool notify = false)
35233524
}
35243525
}
35253526
else // Otherwise, the client did not find the client to server scene handle
3526-
if (NetworkManagerOwner.LogLevel == LogLevel.Developer)
3527+
if (NetworkManagerOwner.LogLevel <= LogLevel.Developer)
35273528
{
35283529
// There could be a scenario where a user has some client-local scene loaded that they migrate the NetworkObject
35293530
// into, but that scenario seemed very edge case and under most instances a user should be notified that this
@@ -3642,7 +3643,7 @@ internal void OnNetworkBehaviourDestroyed(NetworkBehaviour networkBehaviour)
36423643
{
36433644
if (networkBehaviour.IsSpawned && IsSpawned)
36443645
{
3645-
if (NetworkManagerOwner?.LogLevel == LogLevel.Developer)
3646+
if (NetworkManagerOwner.LogLevel <= LogLevel.Developer)
36463647
{
36473648
NetworkLog.LogWarning($"{nameof(NetworkBehaviour)}-{networkBehaviour.name} is being destroyed while {nameof(NetworkObject)}-{name} is still spawned! (could break state synchronization)");
36483649
}

0 commit comments

Comments
 (0)