Skip to content

Commit aab3f4d

Browse files
committed
Check cached usage and cleanup
1 parent e903ba8 commit aab3f4d

File tree

1 file changed

+44
-45
lines changed

1 file changed

+44
-45
lines changed

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

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -340,17 +340,18 @@ private void CheckForInScenePlaced()
340340

341341
internal bool HasParentNetworkObject(Transform transform)
342342
{
343-
if (m_CachedParent != null)
343+
var parent = transform.parent;
344+
if (parent != null)
344345
{
345-
var networkObject = m_CachedParent.GetComponent<NetworkObject>();
346+
var networkObject = parent.GetComponent<NetworkObject>();
346347
if (networkObject != null && networkObject != this)
347348
{
348349
return true;
349350
}
350351

351352
if (m_CachedParent.parent != null)
352353
{
353-
return HasParentNetworkObject(m_CachedParent);
354+
return HasParentNetworkObject(parent);
354355
}
355356
}
356357
return false;
@@ -400,7 +401,7 @@ public void DeferDespawn(int tickOffset, bool destroy = true)
400401
{
401402
if (!IsSpawned)
402403
{
403-
NetworkLog.LogError($"Cannot defer despawning {name} because it is not spawned!");
404+
NetworkLog.LogError($"Cannot defer despawn because [{name}] is not spawned!");
404405
return;
405406
}
406407

@@ -770,7 +771,7 @@ public OwnershipRequestStatus RequestOwnership()
770771
{
771772
if (NetworkManagerOwner.LogLevel <= LogLevel.Normal)
772773
{
773-
NetworkLog.LogWarning($"Trying to perform an ownership request but {name} is not spawned!");
774+
NetworkLog.LogWarning($"Trying to perform an ownership request but [{name}] is not spawned!");
774775
}
775776

776777
return OwnershipRequestStatus.Invalid;
@@ -1137,6 +1138,10 @@ private bool InternalHasAuthority()
11371138
{
11381139
if (!IsSpawned)
11391140
{
1141+
if (NetworkManagerOwner.LogLevel <= LogLevel.Normal)
1142+
{
1143+
NetworkLog.LogWarning($"Trying to check authority but {name} is not spawned yet!");
1144+
}
11401145
return false;
11411146
}
11421147
return NetworkManagerOwner.DistributedAuthorityMode ? OwnerClientId == NetworkManagerOwner.LocalClientId : NetworkManagerOwner.IsServer;
@@ -1200,7 +1205,7 @@ private bool InternalHasAuthority()
12001205
/// <summary>
12011206
/// Gets whether or not the object is owned by anyone
12021207
/// </summary>
1203-
public bool IsOwnedByServer => IsSpawned && OwnerClientId == NetworkManager.ServerClientId;
1208+
public bool IsOwnedByServer => OwnerClientId == NetworkManager.ServerClientId;
12041209

12051210
/// <summary>
12061211
/// Gets if the object has yet been spawned across the network
@@ -1543,9 +1548,9 @@ public static void NetworkShow(List<NetworkObject> networkObjects, ulong clientI
15431548

15441549
// Distributed authority mode adjustments to log a network error and continue when trying to show a NetworkObject
15451550
// that the local instance does not own
1546-
if (!networkObjects[i].HasAuthority)
1551+
if (!networkObject.HasAuthority)
15471552
{
1548-
if (networkObjects[i].NetworkManagerOwner.DistributedAuthorityMode)
1553+
if (networkManager.DistributedAuthorityMode)
15491554
{
15501555
// It will log locally and to the "master-host".
15511556
NetworkLog.LogErrorServer("Only the owner-authority can change visibility when distributed authority mode is enabled!");
@@ -1558,14 +1563,14 @@ public static void NetworkShow(List<NetworkObject> networkObjects, ulong clientI
15581563
}
15591564

15601565

1561-
if (networkObjects[i].Observers.Contains(clientId))
1566+
if (networkObject.Observers.Contains(clientId))
15621567
{
1563-
throw new VisibilityChangeException($"{nameof(NetworkObject)} with NetworkId: {networkObjects[i].NetworkObjectId} is already visible");
1568+
throw new VisibilityChangeException($"{nameof(NetworkObject)} with NetworkId: {networkObject.NetworkObjectId} is already visible");
15641569
}
15651570

1566-
if (networkObjects[i].NetworkManagerOwner != networkManager)
1571+
if (networkObject.NetworkManagerOwner != networkManager)
15671572
{
1568-
throw new ArgumentNullException("All " + nameof(NetworkObject) + "s must belong to the same " + nameof(NetworkManagerOwner));
1573+
throw new ArgumentNullException("All " + nameof(NetworkObject) + "s must belong to the same " + nameof(networkManager));
15691574
}
15701575
}
15711576

@@ -1703,9 +1708,9 @@ public static void NetworkHide(List<NetworkObject> networkObjects, ulong clientI
17031708

17041709
// Distributed authority mode adjustments to log a network error and continue when trying to show a NetworkObject
17051710
// that the local instance does not own
1706-
if (!networkObjects[i].HasAuthority)
1711+
if (!networkObject.HasAuthority)
17071712
{
1708-
if (networkObjects[i].NetworkManagerOwner.DistributedAuthorityMode)
1713+
if (networkObject.NetworkManagerOwner.DistributedAuthorityMode)
17091714
{
17101715
// It will log locally and to the "master-host".
17111716
NetworkLog.LogErrorServer($"Only the owner-authority can change hide a {nameof(NetworkObject)} when distributed authority mode is enabled!");
@@ -1718,19 +1723,19 @@ public static void NetworkHide(List<NetworkObject> networkObjects, ulong clientI
17181723
}
17191724

17201725
// CLIENT SPAWNING TODO: Log error and continue as opposed to throwing an exception
1721-
if (!networkObjects[i].IsSpawned)
1726+
if (!networkObject.IsSpawned)
17221727
{
17231728
throw new SpawnStateException("Object is not spawned");
17241729
}
17251730

1726-
if (!networkObjects[i].Observers.Contains(clientId))
1731+
if (!networkObject.Observers.Contains(clientId))
17271732
{
1728-
throw new VisibilityChangeException($"{nameof(NetworkObject)} with {nameof(NetworkObjectId)}: {networkObjects[i].NetworkObjectId} is already hidden");
1733+
throw new VisibilityChangeException($"{nameof(NetworkObject)} with {nameof(NetworkObjectId)}: {networkObject.NetworkObjectId} is already hidden");
17291734
}
17301735

1731-
if (networkObjects[i].NetworkManagerOwner != networkManager)
1736+
if (networkObject.NetworkManagerOwner != networkManager)
17321737
{
1733-
throw new ArgumentNullException("All " + nameof(NetworkObject) + "s must belong to the same " + nameof(NetworkManagerOwner));
1738+
throw new ArgumentNullException("All " + nameof(NetworkObject) + "s must belong to the same " + nameof(networkManager));
17341739
}
17351740
}
17361741

@@ -1742,7 +1747,6 @@ public static void NetworkHide(List<NetworkObject> networkObjects, ulong clientI
17421747

17431748
private void OnDestroy()
17441749
{
1745-
// TODO Can we destroy an object before it is spawned?
17461750
var networkManager = NetworkManager;
17471751
// If no NetworkManager is assigned, then just exit early
17481752
if (!networkManager)
@@ -2013,7 +2017,7 @@ public void Despawn(bool destroy = true)
20132017
{
20142018
if (!IsSpawned)
20152019
{
2016-
NetworkLog.LogErrorServer("Object is not spawned!");
2020+
NetworkLog.LogErrorServer($"Cannot despawn [{name}] as it is not spawned!");
20172021
return;
20182022
}
20192023

@@ -2041,7 +2045,7 @@ public void RemoveOwnership()
20412045
{
20422046
if (!IsSpawned)
20432047
{
2044-
NetworkLog.LogErrorServer("Trying to remove ownership on an object that is not spawned!");
2048+
NetworkLog.LogErrorServer($"Trying to remove the ownership of [{name}] but it is not spawned!");
20452049
return;
20462050
}
20472051
NetworkManagerOwner.SpawnManager.RemoveOwnership(this);
@@ -2055,7 +2059,7 @@ public void ChangeOwnership(ulong newOwnerClientId)
20552059
{
20562060
if (!IsSpawned)
20572061
{
2058-
NetworkLog.LogWarning("Trying to change ownership on an not spawned object!");
2062+
NetworkLog.LogWarning($"Trying to change ownership on [{name}] but it is not spawned!");
20592063
return;
20602064
}
20612065

@@ -2070,7 +2074,7 @@ internal void InvokeBehaviourOnOwnershipChanged(ulong originalOwnerClientId, ulo
20702074
{
20712075
if (!IsSpawned)
20722076
{
2073-
NetworkLog.LogWarning("Trying to change ownership on an not spawned object!");
2077+
NetworkLog.LogWarning($"Trying to change ownership on [{name}] but it is not spawned!");
20742078
return;
20752079
}
20762080

@@ -2267,7 +2271,7 @@ public bool TryRemoveParent(bool worldPositionStays = true)
22672271
/// </summary>
22682272
/// <param name="parent">The new parent for this NetworkObject transform will be the child of.</param>
22692273
/// <param name="worldPositionStays">If true, the parent-relative position, scale and rotation are modified such that the object keeps the same world space position, rotation and scale as before.</param>
2270-
/// <returns>Whether or not re-parenting was successful.</returns>
2274+
/// <returns>Whether or not reparenting was successful.</returns>
22712275
public bool TrySetParent(NetworkObject parent, bool worldPositionStays = true)
22722276
{
22732277
if (!AutoObjectParentSync || !IsSpawned || !NetworkManagerOwner.IsListening)
@@ -2279,7 +2283,7 @@ public bool TrySetParent(NetworkObject parent, bool worldPositionStays = true)
22792283
// It wouldn't make sense to not allow parenting, but keeping this note here as a reminder.
22802284
var isAuthority = HasAuthority || (AllowOwnerToParent && IsOwner);
22812285

2282-
// If we don't have authority, and we are not shutting down, then don't allow any parenting.
2286+
// If we don't have authority and we are not shutting down, then don't allow any parenting.
22832287
// If we are shutting down and don't have authority then allow it.
22842288
if (!isAuthority && !NetworkManagerOwner.ShutdownInProgress)
22852289
{
@@ -2291,9 +2295,9 @@ public bool TrySetParent(NetworkObject parent, bool worldPositionStays = true)
22912295

22922296
internal bool InternalTrySetParent(NetworkObject parent, bool worldPositionStays = true)
22932297
{
2294-
if (parent && (IsSpawned ^ parent.IsSpawned) && !NetworkManagerOwner.ShutdownInProgress)
2298+
if (parent && (IsSpawned ^ parent.IsSpawned) && !NetworkManager.ShutdownInProgress)
22952299
{
2296-
if (NetworkManagerOwner.LogLevel <= LogLevel.Developer)
2300+
if (NetworkManager.LogLevel <= LogLevel.Developer)
22972301
{
22982302
var nameOfNotSpawnedObject = IsSpawned ? $" the parent ({parent.name})" : $"the child ({name})";
22992303
NetworkLog.LogWarning($"Parenting failed because {nameOfNotSpawnedObject} is not spawned!");
@@ -2303,7 +2307,7 @@ internal bool InternalTrySetParent(NetworkObject parent, bool worldPositionStays
23032307

23042308
m_CachedWorldPositionStays = worldPositionStays;
23052309

2306-
if (!parent)
2310+
if (parent == null)
23072311
{
23082312
CurrentParent = null;
23092313
transform.SetParent(null, worldPositionStays);
@@ -2319,7 +2323,6 @@ internal bool InternalTrySetParent(NetworkObject parent, bool worldPositionStays
23192323

23202324
private void OnTransformParentChanged()
23212325
{
2322-
// Shouldn't we do our if not spawned check here?
23232326
if (!AutoObjectParentSync)
23242327
{
23252328
return;
@@ -2345,7 +2348,7 @@ private void OnTransformParentChanged()
23452348
return;
23462349
}
23472350
transform.parent = m_CachedParent;
2348-
Debug.LogException(new NotListeningException($"{nameof(networkManager)} is not listening, start a server or host before re-parenting"));
2351+
Debug.LogException(new NotListeningException($"{nameof(networkManager)} is not listening, start a server or host before reparenting"));
23492352
return;
23502353
}
23512354
var isAuthority = false;
@@ -2362,18 +2365,17 @@ private void OnTransformParentChanged()
23622365
if (distributedAuthority)
23632366
{
23642367
transform.parent = m_CachedParent;
2365-
NetworkLog.LogError($"[Not Owner] Only the owner-authority of child {gameObject.name}'s {nameof(NetworkObject)} component can re-parent it!");
2368+
NetworkLog.LogError($"[Not Owner] Only the owner-authority of child {gameObject.name}'s {nameof(NetworkObject)} component can reparent it!");
23662369
}
23672370
else
23682371
{
23692372
transform.parent = m_CachedParent;
2370-
Debug.LogException(new NotServerException($"Only the server can re-parent {nameof(NetworkObject)}s"));
2373+
Debug.LogException(new NotServerException($"Only the server can reparent {nameof(NetworkObject)}s"));
23712374
}
23722375
return;
23732376
}
23742377

23752378

2376-
//Shouldn't this be done first? if the object is not, spawned we enter the first if (!IsSpawned || !NetworkManager.IsListening)
23772379
if (!IsSpawned)
23782380
{
23792381
AuthorityAppliedParenting = false;
@@ -2387,7 +2389,7 @@ private void OnTransformParentChanged()
23872389
else
23882390
{
23892391
transform.parent = m_CachedParent;
2390-
Debug.LogException(new SpawnStateException($"{nameof(NetworkObject)} can only be re-parented after being spawned"));
2392+
Debug.LogException(new SpawnStateException($"{nameof(NetworkObject)} can only be reparented after being spawned"));
23912393
}
23922394
return;
23932395
}
@@ -2406,7 +2408,7 @@ private void OnTransformParentChanged()
24062408
{
24072409
transform.parent = m_CachedParent;
24082410
AuthorityAppliedParenting = false;
2409-
Debug.LogException(new SpawnStateException($"{nameof(NetworkObject)} can only be re-parented under another spawned {nameof(NetworkObject)}"));
2411+
Debug.LogException(new SpawnStateException($"{nameof(NetworkObject)} can only be reparented under another spawned {nameof(NetworkObject)}"));
24102412
return;
24112413
}
24122414

@@ -2507,7 +2509,7 @@ internal bool ApplyNetworkParenting(bool removeParent = false, bool ignoreNotSpa
25072509
var parentNetworkObject = transform.parent.GetComponent<NetworkObject>();
25082510

25092511
// If parentNetworkObject is null then the parent is a GameObject without a NetworkObject component
2510-
// attached. Under this case, we preserve the hierarchy, but we don't keep track of the parenting.
2512+
// attached. Under this case, we preserve the hierarchy but we don't keep track of the parenting.
25112513
// Note: We only start tracking parenting if the user removes the child from the standard GameObject
25122514
// parent and then re-parents the child under a GameObject with a NetworkObject component attached.
25132515
if (parentNetworkObject == null)
@@ -2618,7 +2620,7 @@ internal void InvokeBehaviourNetworkSpawn()
26182620
{
26192621
if (!IsSpawned)
26202622
{
2621-
Debug.LogError("Not spawned!");
2623+
NetworkLog.LogWarning($"Trying to invoke network spawn behavior on {name} but is not spawned!");
26222624
return;
26232625
}
26242626
NetworkManagerOwner.SpawnManager.UpdateOwnershipTable(this, OwnerClientId);
@@ -2688,7 +2690,7 @@ internal void InvokeBehaviourNetworkDespawn()
26882690
{
26892691
if (!IsSpawned)
26902692
{
2691-
Debug.LogError("Cannot despawn if is not spawned!");
2693+
NetworkLog.LogWarning($"Trying to invoke network despawn behavior on {name} but is not spawned!");
26922694
return;
26932695
}
26942696
// Invoke OnNetworkPreDespawn on all child behaviours
@@ -2782,7 +2784,7 @@ internal void SynchronizeOwnerNetworkVariables(ulong originalOwnerId, ulong orig
27822784
{
27832785
if (!IsSpawned)
27842786
{
2785-
Debug.LogError("Cannot synchronize network variables if not spawned!");
2787+
NetworkLog.LogWarning($"Trying to synchronize network variables on {name} but is not spawned!");
27862788
return;
27872789
}
27882790
var currentOwnerId = OwnerClientId;
@@ -3019,7 +3021,6 @@ public struct TransformData : INetworkSerializeByMemcpy
30193021

30203022
public void Serialize(FastBufferWriter writer)
30213023
{
3022-
// CHECK should we check if OwnerObject.IsSpawned here?
30233024
if (OwnerObject.NetworkManager.DistributedAuthorityMode)
30243025
{
30253026
HasOwnershipFlags = true;
@@ -3235,7 +3236,6 @@ internal void SynchronizeNetworkBehaviours<T>(ref BufferSerializer<T> serializer
32353236

32363237
internal SceneObject GetMessageSceneObject(ulong targetClientId = NetworkManager.ServerClientId, bool syncObservers = false)
32373238
{
3238-
// CHECK can we use NetworkManagerOwner here? if we do, also replace here:CheckForGlobalObjectIdHashOverride
32393239
var obj = new SceneObject
32403240
{
32413241
HasParent = transform.parent != null,
@@ -3508,9 +3508,8 @@ private void CurrentlyActiveSceneChanged(Scene current, Scene next)
35083508
/// </summary>
35093509
internal void SceneChangedUpdate(Scene scene, bool notify = false)
35103510
{
3511-
//CHECK
3512-
// Avoiding edge case scenarios, if no NetworkSceneManager exit early
3513-
if (NetworkManagerOwner.SceneManager == null || !IsSpawned)
3511+
// Avoiding edge case scenarios, if not spawned or no NetworkSceneManager exit early
3512+
if (!IsSpawned || NetworkManagerOwner.SceneManager == null)
35143513
{
35153514
return;
35163515
}

0 commit comments

Comments
 (0)