You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix: networkvariablebase not being reinitialized if networkobject persists between sessions (#3181)
* fix
This will "re-initialize" NetworkVariableBase derived classes when the instance is not destroyed and repurposed.
* update
adding changelog entry
* update
Adding PR number to changelog entry
* fix
? This test needs more comments.
* update
Changing the approach to this fix while also cleaning up the initialize process so it can be invoked when the derived class is not yet fully spawned (or just recently instantiated) without potentially assigning the wrong NetworkManager instance. It is being invoked multiple times anyway, so this just gradually initializes all of the required elements until it is finally considered "initialized".
This also prevents OnInitialize from being invoked multiple times during this process and OnInitialize is only invoked as a last step to being considered "initialized".
Added a Deinitialize step to NetworkVariableBase when despawning a NetworkObject where the initialized status is reset. This allows for the next spawning of the NetworkObject (if in-scene placed or pooled) to run through the initialization process.
* test
reverting the changes to WhenServerChangesSmoothValue_ValuesAreLerped as the recent update resolves the issue from the previous fix.
* update
Removing exception.
Adjusting when we update the last time.
Logging warning if log level is developer and there is no NetworkTimeSystem.
* update
remove the try catch because evidently it is the only way we can catch certain exceptions during unit testing when duplicating the value (i.e. serializing).
* test
This validates the fix for resetting the NetworkVariableBase.LastUpdated property when a NetworkObject is persisted (i.e. recycled via in-scene placed NetworkObject or object pools).
Also minor update to prevent the integration test from logging a blank entry if there has been no logs added.
* test
The first iteration should ignore the time delta as that could be impacted by the test or VM running the test.
Copy file name to clipboardexpand all lines: com.unity.netcode.gameobjects/CHANGELOG.md
+1
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
14
14
15
15
- Changed the `NetworkTimeSystem.Sync` method to use half RTT to calculate the desired local time offset as opposed to the full RTT. (#3212)
16
16
- Fixed issue where a spawned `NetworkObject` that was registered with a prefab handler and owned by a client would invoke destroy more than once on the host-server side if the client disconnected while the `NetworkObject` was still spawned. (#3200)
17
+
- Fixed issue where `NetworkVariableBase` derived classes were not being re-initialized if the associated `NetworkObject` instance was not destroyed and re-spawned. (#3181)
Copy file name to clipboardexpand all lines: com.unity.netcode.gameobjects/Runtime/NetworkVariable/NetworkVariableBase.cs
+72-20
Original file line number
Diff line number
Diff line change
@@ -35,6 +35,12 @@ public abstract class NetworkVariableBase : IDisposable
35
35
36
36
privateNetworkManagerm_InternalNetworkManager;
37
37
38
+
// Determines if this NetworkVariable has been "initialized" to prevent initializing more than once which can happen when first
39
+
// instantiated and spawned. If this NetworkVariable instance is on an in-scene placed NetworkObject =or= a pooled NetworkObject
40
+
// that can persist between sessions and/or be recycled we need to reset the LastUpdateSent value prior to spawning otherwise
41
+
// this NetworkVariableBase property instance will not update until the last session time used.
42
+
internalboolHasBeenInitialized{get;privateset;}
43
+
38
44
internalstringGetWritePermissionError()
39
45
{
40
46
return$"|Client-{m_NetworkManager.LocalClientId}|{m_NetworkBehaviour.name}|{Name}| Write permissions ({WritePerm}) for this client instance is not allowed!";
Debug.LogWarning($"[{m_NetworkBehaviour.name}][{m_NetworkBehaviour.GetType().Name}][{GetType().Name}][Initialize] {nameof(NetworkManager)} has no {nameof(NetworkTimeSystem)} assigned!");
125
+
}
126
+
}
127
+
128
+
/// <summary>
129
+
/// Deinitialize is invoked when a NetworkObject is despawned.
130
+
/// This allows for a recyled NetworkObject (in-scene or pooled)
131
+
/// to be properly initialized upon the next use/spawn.
132
+
/// </summary>
133
+
internalvoidDeinitialize()
134
+
{
135
+
// When despawned, reset the HasBeenInitialized so if the associated NetworkObject instance
136
+
// is recylced (i.e. in-scene placed or pooled) it will re-initialize the LastUpdateSent time.
0 commit comments