Skip to content

Commit 02d061d

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

File tree

1 file changed

+45
-45
lines changed

1 file changed

+45
-45
lines changed

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

Lines changed: 45 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,20 @@ 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+
// CHECK, should I replace this?
1727+
if (!networkObject.IsSpawned)
17221728
{
17231729
throw new SpawnStateException("Object is not spawned");
17241730
}
17251731

1726-
if (!networkObjects[i].Observers.Contains(clientId))
1732+
if (!networkObject.Observers.Contains(clientId))
17271733
{
1728-
throw new VisibilityChangeException($"{nameof(NetworkObject)} with {nameof(NetworkObjectId)}: {networkObjects[i].NetworkObjectId} is already hidden");
1734+
throw new VisibilityChangeException($"{nameof(NetworkObject)} with {nameof(NetworkObjectId)}: {networkObject.NetworkObjectId} is already hidden");
17291735
}
17301736

1731-
if (networkObjects[i].NetworkManagerOwner != networkManager)
1737+
if (networkObject.NetworkManagerOwner != networkManager)
17321738
{
1733-
throw new ArgumentNullException("All " + nameof(NetworkObject) + "s must belong to the same " + nameof(NetworkManagerOwner));
1739+
throw new ArgumentNullException("All " + nameof(NetworkObject) + "s must belong to the same " + nameof(networkManager));
17341740
}
17351741
}
17361742

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

17431749
private void OnDestroy()
17441750
{
1745-
// TODO Can we destroy an object before it is spawned?
17461751
var networkManager = NetworkManager;
17471752
// If no NetworkManager is assigned, then just exit early
17481753
if (!networkManager)
@@ -2013,7 +2018,7 @@ public void Despawn(bool destroy = true)
20132018
{
20142019
if (!IsSpawned)
20152020
{
2016-
NetworkLog.LogErrorServer("Object is not spawned!");
2021+
NetworkLog.LogErrorServer($"Cannot despawn [{name}] as it is not spawned!");
20172022
return;
20182023
}
20192024

@@ -2041,7 +2046,7 @@ public void RemoveOwnership()
20412046
{
20422047
if (!IsSpawned)
20432048
{
2044-
NetworkLog.LogErrorServer("Trying to remove ownership on an object that is not spawned!");
2049+
NetworkLog.LogErrorServer($"Trying to remove the ownership of [{name}] but it is not spawned!");
20452050
return;
20462051
}
20472052
NetworkManagerOwner.SpawnManager.RemoveOwnership(this);
@@ -2055,7 +2060,7 @@ public void ChangeOwnership(ulong newOwnerClientId)
20552060
{
20562061
if (!IsSpawned)
20572062
{
2058-
NetworkLog.LogWarning("Trying to change ownership on an not spawned object!");
2063+
NetworkLog.LogWarning($"Trying to change ownership on [{name}] but it is not spawned!");
20592064
return;
20602065
}
20612066

@@ -2070,7 +2075,7 @@ internal void InvokeBehaviourOnOwnershipChanged(ulong originalOwnerClientId, ulo
20702075
{
20712076
if (!IsSpawned)
20722077
{
2073-
NetworkLog.LogWarning("Trying to change ownership on an not spawned object!");
2078+
NetworkLog.LogWarning($"Trying to change ownership on [{name}] but it is not spawned!");
20742079
return;
20752080
}
20762081

@@ -2267,7 +2272,7 @@ public bool TryRemoveParent(bool worldPositionStays = true)
22672272
/// </summary>
22682273
/// <param name="parent">The new parent for this NetworkObject transform will be the child of.</param>
22692274
/// <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>
2275+
/// <returns>Whether or not reparenting was successful.</returns>
22712276
public bool TrySetParent(NetworkObject parent, bool worldPositionStays = true)
22722277
{
22732278
if (!AutoObjectParentSync || !IsSpawned || !NetworkManagerOwner.IsListening)
@@ -2279,7 +2284,7 @@ public bool TrySetParent(NetworkObject parent, bool worldPositionStays = true)
22792284
// It wouldn't make sense to not allow parenting, but keeping this note here as a reminder.
22802285
var isAuthority = HasAuthority || (AllowOwnerToParent && IsOwner);
22812286

2282-
// If we don't have authority, and we are not shutting down, then don't allow any parenting.
2287+
// If we don't have authority and we are not shutting down, then don't allow any parenting.
22832288
// If we are shutting down and don't have authority then allow it.
22842289
if (!isAuthority && !NetworkManagerOwner.ShutdownInProgress)
22852290
{
@@ -2291,9 +2296,9 @@ public bool TrySetParent(NetworkObject parent, bool worldPositionStays = true)
22912296

22922297
internal bool InternalTrySetParent(NetworkObject parent, bool worldPositionStays = true)
22932298
{
2294-
if (parent && (IsSpawned ^ parent.IsSpawned) && !NetworkManagerOwner.ShutdownInProgress)
2299+
if (parent && (IsSpawned ^ parent.IsSpawned) && !NetworkManager.ShutdownInProgress)
22952300
{
2296-
if (NetworkManagerOwner.LogLevel <= LogLevel.Developer)
2301+
if (NetworkManager.LogLevel <= LogLevel.Developer)
22972302
{
22982303
var nameOfNotSpawnedObject = IsSpawned ? $" the parent ({parent.name})" : $"the child ({name})";
22992304
NetworkLog.LogWarning($"Parenting failed because {nameOfNotSpawnedObject} is not spawned!");
@@ -2303,7 +2308,7 @@ internal bool InternalTrySetParent(NetworkObject parent, bool worldPositionStays
23032308

23042309
m_CachedWorldPositionStays = worldPositionStays;
23052310

2306-
if (!parent)
2311+
if (parent == null)
23072312
{
23082313
CurrentParent = null;
23092314
transform.SetParent(null, worldPositionStays);
@@ -2319,7 +2324,6 @@ internal bool InternalTrySetParent(NetworkObject parent, bool worldPositionStays
23192324

23202325
private void OnTransformParentChanged()
23212326
{
2322-
// Shouldn't we do our if not spawned check here?
23232327
if (!AutoObjectParentSync)
23242328
{
23252329
return;
@@ -2345,7 +2349,7 @@ private void OnTransformParentChanged()
23452349
return;
23462350
}
23472351
transform.parent = m_CachedParent;
2348-
Debug.LogException(new NotListeningException($"{nameof(networkManager)} is not listening, start a server or host before re-parenting"));
2352+
Debug.LogException(new NotListeningException($"{nameof(networkManager)} is not listening, start a server or host before reparenting"));
23492353
return;
23502354
}
23512355
var isAuthority = false;
@@ -2362,18 +2366,17 @@ private void OnTransformParentChanged()
23622366
if (distributedAuthority)
23632367
{
23642368
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!");
2369+
NetworkLog.LogError($"[Not Owner] Only the owner-authority of child {gameObject.name}'s {nameof(NetworkObject)} component can reparent it!");
23662370
}
23672371
else
23682372
{
23692373
transform.parent = m_CachedParent;
2370-
Debug.LogException(new NotServerException($"Only the server can re-parent {nameof(NetworkObject)}s"));
2374+
Debug.LogException(new NotServerException($"Only the server can reparent {nameof(NetworkObject)}s"));
23712375
}
23722376
return;
23732377
}
23742378

23752379

2376-
//Shouldn't this be done first? if the object is not, spawned we enter the first if (!IsSpawned || !NetworkManager.IsListening)
23772380
if (!IsSpawned)
23782381
{
23792382
AuthorityAppliedParenting = false;
@@ -2387,7 +2390,7 @@ private void OnTransformParentChanged()
23872390
else
23882391
{
23892392
transform.parent = m_CachedParent;
2390-
Debug.LogException(new SpawnStateException($"{nameof(NetworkObject)} can only be re-parented after being spawned"));
2393+
Debug.LogException(new SpawnStateException($"{nameof(NetworkObject)} can only be reparented after being spawned"));
23912394
}
23922395
return;
23932396
}
@@ -2406,7 +2409,7 @@ private void OnTransformParentChanged()
24062409
{
24072410
transform.parent = m_CachedParent;
24082411
AuthorityAppliedParenting = false;
2409-
Debug.LogException(new SpawnStateException($"{nameof(NetworkObject)} can only be re-parented under another spawned {nameof(NetworkObject)}"));
2412+
Debug.LogException(new SpawnStateException($"{nameof(NetworkObject)} can only be reparented under another spawned {nameof(NetworkObject)}"));
24102413
return;
24112414
}
24122415

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

25092512
// 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.
2513+
// attached. Under this case, we preserve the hierarchy but we don't keep track of the parenting.
25112514
// Note: We only start tracking parenting if the user removes the child from the standard GameObject
25122515
// parent and then re-parents the child under a GameObject with a NetworkObject component attached.
25132516
if (parentNetworkObject == null)
@@ -2618,7 +2621,7 @@ internal void InvokeBehaviourNetworkSpawn()
26182621
{
26192622
if (!IsSpawned)
26202623
{
2621-
Debug.LogError("Not spawned!");
2624+
NetworkLog.LogWarning($"Trying to invoke network spawn behavior on {name} but is not spawned!");
26222625
return;
26232626
}
26242627
NetworkManagerOwner.SpawnManager.UpdateOwnershipTable(this, OwnerClientId);
@@ -2688,7 +2691,7 @@ internal void InvokeBehaviourNetworkDespawn()
26882691
{
26892692
if (!IsSpawned)
26902693
{
2691-
Debug.LogError("Cannot despawn if is not spawned!");
2694+
NetworkLog.LogWarning($"Trying to invoke network despawn behavior on {name} but is not spawned!");
26922695
return;
26932696
}
26942697
// Invoke OnNetworkPreDespawn on all child behaviours
@@ -2782,7 +2785,7 @@ internal void SynchronizeOwnerNetworkVariables(ulong originalOwnerId, ulong orig
27822785
{
27832786
if (!IsSpawned)
27842787
{
2785-
Debug.LogError("Cannot synchronize network variables if not spawned!");
2788+
NetworkLog.LogWarning($"Trying to synchronize network variables on {name} but is not spawned!");
27862789
return;
27872790
}
27882791
var currentOwnerId = OwnerClientId;
@@ -3019,7 +3022,6 @@ public struct TransformData : INetworkSerializeByMemcpy
30193022

30203023
public void Serialize(FastBufferWriter writer)
30213024
{
3022-
// CHECK should we check if OwnerObject.IsSpawned here?
30233025
if (OwnerObject.NetworkManager.DistributedAuthorityMode)
30243026
{
30253027
HasOwnershipFlags = true;
@@ -3235,7 +3237,6 @@ internal void SynchronizeNetworkBehaviours<T>(ref BufferSerializer<T> serializer
32353237

32363238
internal SceneObject GetMessageSceneObject(ulong targetClientId = NetworkManager.ServerClientId, bool syncObservers = false)
32373239
{
3238-
// CHECK can we use NetworkManagerOwner here? if we do, also replace here:CheckForGlobalObjectIdHashOverride
32393240
var obj = new SceneObject
32403241
{
32413242
HasParent = transform.parent != null,
@@ -3508,9 +3509,8 @@ private void CurrentlyActiveSceneChanged(Scene current, Scene next)
35083509
/// </summary>
35093510
internal void SceneChangedUpdate(Scene scene, bool notify = false)
35103511
{
3511-
//CHECK
3512-
// Avoiding edge case scenarios, if no NetworkSceneManager exit early
3513-
if (NetworkManagerOwner.SceneManager == null || !IsSpawned)
3512+
// Avoiding edge case scenarios, if not spawned or no NetworkSceneManager exit early
3513+
if (!IsSpawned || NetworkManagerOwner.SceneManager == null)
35143514
{
35153515
return;
35163516
}

0 commit comments

Comments
 (0)