-
Notifications
You must be signed in to change notification settings - Fork 461
fix: Nested NetworkBehaviours don't de-register or Invoke OnNetworkDespawn if destroyed while the parent NetworkObject remains spawned [NCCBUG-137] #2091
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 14 commits
a331aa9
8b00a04
abcef0d
e9fa1ec
5b2002c
37804a7
665a519
77e903a
a866232
ba69043
52e73a0
1b384c7
5c8945c
05956e5
f453080
ad7cfa3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -331,7 +331,8 @@ public NetworkObject NetworkObject | |
| // in Update and/or in FixedUpdate could still be checking NetworkBehaviour.NetworkObject directly (i.e. does it exist?) | ||
| // or NetworkBehaviour.IsSpawned (i.e. to early exit if not spawned) which, in turn, could generate several Warning messages | ||
| // per spawned NetworkObject. Checking for ShutdownInProgress prevents these unnecessary LogWarning messages. | ||
| if (m_NetworkObject == null && (NetworkManager.Singleton == null || !NetworkManager.Singleton.ShutdownInProgress)) | ||
| // We must check IsSpawned, otherwise a warning will be logged under certain valid conditions (see OnDestroy) | ||
| if (IsSpawned && m_NetworkObject == null && (NetworkManager.Singleton == null || !NetworkManager.Singleton.ShutdownInProgress)) | ||
| { | ||
| if (NetworkLog.CurrentLogLevel <= LogLevel.Normal) | ||
| { | ||
|
|
@@ -759,6 +760,14 @@ protected NetworkObject GetNetworkObject(ulong networkId) | |
| /// </summary> | ||
| public virtual void OnDestroy() | ||
| { | ||
| if (NetworkObject != null && NetworkObject.IsSpawned && IsSpawned) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we care if |
||
| { | ||
| // If the associated NetworkObject is still spawned then this | ||
| // NetworkBehaviour will be removed from the NetworkObject's | ||
| // ChildNetworkBehaviours list. | ||
| NetworkObject.OnNetworkBehaviourDestroyed(this); | ||
| } | ||
|
|
||
| // this seems odd to do here, but in fact especially in tests we can find ourselves | ||
| // here without having called InitializedVariables, which causes problems if any | ||
| // of those variables use native containers (e.g. NetworkList) as they won't be | ||
|
|
@@ -770,6 +779,7 @@ public virtual void OnDestroy() | |
| InitializeVariables(); | ||
| } | ||
|
|
||
|
|
||
| for (int i = 0; i < NetworkVariableFields.Count; i++) | ||
| { | ||
| NetworkVariableFields[i].Dispose(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1219,5 +1219,21 @@ internal uint HostCheckForGlobalObjectIdHashOverride() | |
|
|
||
| return GlobalObjectIdHash; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Removes a NetworkBehaviour from the ChildNetworkBehaviours list when destroyed | ||
| /// while the NetworkObject is still spawned. | ||
| /// </summary> | ||
| internal void OnNetworkBehaviourDestroyed(NetworkBehaviour networkBehaviour) | ||
| { | ||
| if (networkBehaviour.IsSpawned && IsSpawned) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, why do we care if the NB IsSpawned - isn't it just the NO that matters? |
||
| { | ||
| if (NetworkManager.LogLevel == LogLevel.Developer) | ||
| { | ||
| NetworkLog.LogWarning($"{nameof(NetworkBehaviour)}-{networkBehaviour.name} is being destroyed while {nameof(NetworkObject)}-{name} is still spawned! (could break state synchronization)"); | ||
| } | ||
| ChildNetworkBehaviours.Remove(networkBehaviour); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.