Skip to content

Commit cf10390

Browse files
committed
Address feedback
1 parent aab3f4d commit cf10390

File tree

1 file changed

+56
-42
lines changed

1 file changed

+56
-42
lines changed

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

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,11 @@ public void DeferDespawn(int tickOffset, bool destroy = true)
401401
{
402402
if (!IsSpawned)
403403
{
404-
NetworkLog.LogError($"Cannot defer despawn because [{name}] is not spawned!");
404+
if (NetworkManager.LogLevel == LogLevel.Error)
405+
{
406+
NetworkLog.LogErrorServer($"[Attempted deferred despawn while un-spawned]");
407+
}
408+
405409
return;
406410
}
407411

@@ -604,7 +608,11 @@ public bool SetOwnershipLock(bool lockOwnership = true)
604608
{
605609
if (!IsSpawned)
606610
{
607-
Debug.LogError($"Trying to add or remove ownership lock on [{name}] which is not spawned!");
611+
if (NetworkManager.LogLevel == LogLevel.Error)
612+
{
613+
NetworkLog.LogErrorServer("[Attempted Lock While un-spawned]");
614+
}
615+
608616
return false;
609617
}
610618

@@ -743,11 +751,6 @@ public enum OwnershipRequestStatus
743751
/// This object is marked as SessionOwnerOnly and therefore cannot be requested
744752
/// </summary>
745753
SessionOwnerOnly,
746-
747-
/// <summary>
748-
/// The request is invalid (if not spawned for instance)
749-
/// </summary>
750-
Invalid,
751754
}
752755

753756
/// <summary>
@@ -771,10 +774,10 @@ public OwnershipRequestStatus RequestOwnership()
771774
{
772775
if (NetworkManagerOwner.LogLevel <= LogLevel.Normal)
773776
{
774-
NetworkLog.LogWarning($"Trying to perform an ownership request but [{name}] is not spawned!");
777+
NetworkLog.LogErrorServer("[Attempted ownership request while un-spawned]");
775778
}
776779

777-
return OwnershipRequestStatus.Invalid;
780+
return OwnershipRequestStatus.Locked;
778781
}
779782
// Exit early the local client is already the owner
780783
if (OwnerClientId == NetworkManagerOwner.LocalClientId)
@@ -1138,9 +1141,9 @@ private bool InternalHasAuthority()
11381141
{
11391142
if (!IsSpawned)
11401143
{
1141-
if (NetworkManagerOwner.LogLevel <= LogLevel.Normal)
1144+
if (NetworkManager.LogLevel == LogLevel.Error)
11421145
{
1143-
NetworkLog.LogWarning($"Trying to check authority but {name} is not spawned yet!");
1146+
NetworkLog.LogErrorServer("[Attempted authority check while un-spawned]");
11441147
}
11451148
return false;
11461149
}
@@ -1528,14 +1531,15 @@ public static void NetworkShow(List<NetworkObject> networkObjects, ulong clientI
15281531
// Do the safety loop first to prevent putting the netcode in an invalid state.
15291532
for (int i = 0; i < networkObjects.Count; i++)
15301533
{
1531-
if (!networkObjects[i].IsSpawned)
1534+
var networkObject = networkObjects[i];
1535+
var networkManager = networkObject.NetworkManagerOwner;
1536+
1537+
if (!networkObject.IsSpawned)
15321538
{
1539+
// CHECK replace this by NetworkLog.LogWarning($"Trying to show object but is not spawned!"); continue; (change all in another PR)
15331540
throw new SpawnStateException("Object is not spawned");
15341541
}
15351542

1536-
var networkObject = networkObjects[i];
1537-
var networkManager = networkObject.NetworkManagerOwner;
1538-
15391543
if (networkManager.DistributedAuthorityMode && clientId == networkObject.OwnerClientId)
15401544
{
15411545
NetworkLog.LogErrorServer($"Cannot hide an object from the owner when distributed authority mode is enabled! (Skipping {networkObject.gameObject.name})");
@@ -1722,6 +1726,7 @@ public static void NetworkHide(List<NetworkObject> networkObjects, ulong clientI
17221726
}
17231727
}
17241728

1729+
// CHECK, should I address this comment and replace all exceptions with log warnings/errors? (yes, in another PR, same as the comment below)
17251730
// CLIENT SPAWNING TODO: Log error and continue as opposed to throwing an exception
17261731
if (!networkObject.IsSpawned)
17271732
{
@@ -2017,7 +2022,11 @@ public void Despawn(bool destroy = true)
20172022
{
20182023
if (!IsSpawned)
20192024
{
2020-
NetworkLog.LogErrorServer($"Cannot despawn [{name}] as it is not spawned!");
2025+
if (NetworkManager.LogLevel == LogLevel.Error)
2026+
{
2027+
NetworkLog.LogErrorServer($"[Attempted despawn while un-spawned]");
2028+
}
2029+
20212030
return;
20222031
}
20232032

@@ -2045,7 +2054,10 @@ public void RemoveOwnership()
20452054
{
20462055
if (!IsSpawned)
20472056
{
2048-
NetworkLog.LogErrorServer($"Trying to remove the ownership of [{name}] but it is not spawned!");
2057+
if (NetworkManager.LogLevel == LogLevel.Error)
2058+
{
2059+
NetworkLog.LogErrorServer("[Attempted ownership removal while un-spawned]");
2060+
}
20492061
return;
20502062
}
20512063
NetworkManagerOwner.SpawnManager.RemoveOwnership(this);
@@ -2059,7 +2071,11 @@ public void ChangeOwnership(ulong newOwnerClientId)
20592071
{
20602072
if (!IsSpawned)
20612073
{
2062-
NetworkLog.LogWarning($"Trying to change ownership on [{name}] but it is not spawned!");
2074+
if (NetworkManager.LogLevel == LogLevel.Error)
2075+
{
2076+
NetworkLog.LogErrorServer("[Attempted ownership change while un-spawned]");
2077+
}
2078+
20632079
return;
20642080
}
20652081

@@ -2074,7 +2090,11 @@ internal void InvokeBehaviourOnOwnershipChanged(ulong originalOwnerClientId, ulo
20742090
{
20752091
if (!IsSpawned)
20762092
{
2077-
NetworkLog.LogWarning($"Trying to change ownership on [{name}] but it is not spawned!");
2093+
if (NetworkManager.LogLevel == LogLevel.Error)
2094+
{
2095+
NetworkLog.LogErrorServer($"[Attempted behavior invoke on ownership changed while un-spawned]");
2096+
}
2097+
20782098
return;
20792099
}
20802100

@@ -2210,7 +2230,7 @@ internal void SetNetworkParenting(ulong? latestParent, bool worldPositionStays)
22102230
/// </summary>
22112231
/// <param name="parent">The new parent for this NetworkObject transform will be the child of.</param>
22122232
/// <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>
2213-
/// <returns>Whether or not reparenting was successful.</returns>
2233+
/// <returns>Whether or not re-parenting was successful.</returns>
22142234
public bool TrySetParent(Transform parent, bool worldPositionStays = true)
22152235
{
22162236
// If we are removing ourself from a parent
@@ -2375,7 +2395,6 @@ private void OnTransformParentChanged()
23752395
return;
23762396
}
23772397

2378-
23792398
if (!IsSpawned)
23802399
{
23812400
AuthorityAppliedParenting = false;
@@ -2618,11 +2637,6 @@ internal void InvokeBehaviourNetworkPreSpawn()
26182637

26192638
internal void InvokeBehaviourNetworkSpawn()
26202639
{
2621-
if (!IsSpawned)
2622-
{
2623-
NetworkLog.LogWarning($"Trying to invoke network spawn behavior on {name} but is not spawned!");
2624-
return;
2625-
}
26262640
NetworkManagerOwner.SpawnManager.UpdateOwnershipTable(this, OwnerClientId);
26272641

26282642
// Always invoke all InternalOnNetworkSpawn methods on each child NetworkBehaviour
@@ -2690,7 +2704,11 @@ internal void InvokeBehaviourNetworkDespawn()
26902704
{
26912705
if (!IsSpawned)
26922706
{
2693-
NetworkLog.LogWarning($"Trying to invoke network despawn behavior on {name} but is not spawned!");
2707+
if (NetworkManager.LogLevel == LogLevel.Error)
2708+
{
2709+
NetworkLog.LogErrorServer("[Attempted network despawn behavior invoke while un-spawned]");
2710+
}
2711+
26942712
return;
26952713
}
26962714
// Invoke OnNetworkPreDespawn on all child behaviours
@@ -2782,11 +2800,6 @@ internal List<NetworkBehaviour> ChildNetworkBehaviours
27822800
/// <param name="originalPreviousOwnerId">the previous owner prior to beginning the change in ownership change.</param>
27832801
internal void SynchronizeOwnerNetworkVariables(ulong originalOwnerId, ulong originalPreviousOwnerId)
27842802
{
2785-
if (!IsSpawned)
2786-
{
2787-
NetworkLog.LogWarning($"Trying to synchronize network variables on {name} but is not spawned!");
2788-
return;
2789-
}
27902803
var currentOwnerId = OwnerClientId;
27912804
OwnerClientId = originalOwnerId;
27922805
PreviousOwnerId = originalPreviousOwnerId;
@@ -3021,7 +3034,7 @@ public struct TransformData : INetworkSerializeByMemcpy
30213034

30223035
public void Serialize(FastBufferWriter writer)
30233036
{
3024-
if (OwnerObject.NetworkManager.DistributedAuthorityMode)
3037+
if (OwnerObject.NetworkManagerOwner.DistributedAuthorityMode)
30253038
{
30263039
HasOwnershipFlags = true;
30273040
SpawnWithObservers = OwnerObject.SpawnWithObservers;
@@ -3070,7 +3083,7 @@ public void Serialize(FastBufferWriter writer)
30703083

30713084
// The NetworkSceneHandle is the server-side relative
30723085
// scene handle that the NetworkObject resides in.
3073-
if (OwnerObject.NetworkManager.DistributedAuthorityMode)
3086+
if (OwnerObject.NetworkManagerOwner.DistributedAuthorityMode)
30743087
{
30753088
writer.WriteValue(OwnerObject.NetworkSceneHandle);
30763089
}
@@ -3246,7 +3259,7 @@ internal SceneObject GetMessageSceneObject(ulong targetClientId = NetworkManager
32463259
IsSceneObject = IsSceneObject ?? true,
32473260
DestroyWithScene = DestroyWithScene,
32483261
DontDestroyWithOwner = DontDestroyWithOwner,
3249-
HasOwnershipFlags = NetworkManager.DistributedAuthorityMode,
3262+
HasOwnershipFlags = NetworkManagerOwner.DistributedAuthorityMode,
32503263
OwnershipFlags = (ushort)Ownership,
32513264
SyncObservers = syncObservers,
32523265
Observers = syncObservers ? Observers.ToArray() : null,
@@ -3554,7 +3567,7 @@ internal void SceneChangedUpdate(Scene scene, bool notify = false)
35543567
OnMigratedToNewScene?.Invoke();
35553568

35563569
// Only the authority side will notify clients of non-parented NetworkObject scene changes
3557-
if (isAuthority && notify && !m_CachedParent)
3570+
if (isAuthority && notify && !transform.parent)
35583571
{
35593572
NetworkManagerOwner.SceneManager.NotifyNetworkObjectSceneChanged(this);
35603573
}
@@ -3612,17 +3625,18 @@ internal bool UpdateForSceneChanges()
36123625
/// <returns>appropriate hash value</returns>
36133626
internal uint CheckForGlobalObjectIdHashOverride()
36143627
{
3615-
if (NetworkManager.IsServer || NetworkManager.DistributedAuthorityMode)
3628+
var networkManager = NetworkManager;
3629+
if (networkManager.IsServer || networkManager.DistributedAuthorityMode)
36163630
{
3617-
if (NetworkManager.PrefabHandler.ContainsHandler(this))
3631+
if (networkManager.PrefabHandler.ContainsHandler(this))
36183632
{
3619-
var globalObjectIdHash = NetworkManager.PrefabHandler.GetSourceGlobalObjectIdHash(GlobalObjectIdHash);
3633+
var globalObjectIdHash = networkManager.PrefabHandler.GetSourceGlobalObjectIdHash(GlobalObjectIdHash);
36203634
return globalObjectIdHash == 0 ? GlobalObjectIdHash : globalObjectIdHash;
36213635
}
36223636

36233637
// If scene management is disabled and this is an in-scene placed NetworkObject then go ahead
36243638
// and send the InScenePlacedSourcePrefab's GlobalObjectIdHash value (i.e. what to dynamically spawn)
3625-
if (!NetworkManager.NetworkConfig.EnableSceneManagement && IsSceneObject.Value && InScenePlacedSourceGlobalObjectIdHash != 0)
3639+
if (!networkManager.NetworkConfig.EnableSceneManagement && IsSceneObject.Value && InScenePlacedSourceGlobalObjectIdHash != 0)
36263640
{
36273641
return InScenePlacedSourceGlobalObjectIdHash;
36283642
}
@@ -3640,9 +3654,9 @@ internal uint CheckForGlobalObjectIdHashOverride()
36403654
else
36413655
{
36423656
// For legacy manual instantiation and spawning, check the OverrideToNetworkPrefab for a possible match
3643-
if (NetworkManager.NetworkConfig.Prefabs.OverrideToNetworkPrefab.ContainsKey(GlobalObjectIdHash))
3657+
if (networkManager.NetworkConfig.Prefabs.OverrideToNetworkPrefab.ContainsKey(GlobalObjectIdHash))
36443658
{
3645-
return NetworkManager.NetworkConfig.Prefabs.OverrideToNetworkPrefab[GlobalObjectIdHash];
3659+
return networkManager.NetworkConfig.Prefabs.OverrideToNetworkPrefab[GlobalObjectIdHash];
36463660
}
36473661
}
36483662
}

0 commit comments

Comments
 (0)