Skip to content

Commit 95a020e

Browse files
fix: second part
This migrates the NetworkList specific script from NetworkBehaviour.InternalOnNetworkSpawn into NetworkList itself. It also provides (currently) two internal virtual methods, NetworkVariableBase.OnSpawned and NetworkVariableBase.OnPreDespawn, to provide a means for NetworkList to handle cleaning its dirty state up after the instance with write permissions has finished running through the spawn stages (pre-spawn, spawning, post-spawn, gets invoked).
1 parent f3f4eef commit 95a020e

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,13 @@ internal void NetworkPostSpawn()
810810
{
811811
Debug.LogException(e);
812812
}
813+
814+
// Let each NetworkVariableBase derived instance know that
815+
// all spawn related methods have been invoked.
816+
for (int i = 0; i < NetworkVariableFields.Count; i++)
817+
{
818+
NetworkVariableFields[i].OnSpawned();
819+
}
813820
}
814821

815822
internal void NetworkSessionSynchronized()
@@ -847,6 +854,13 @@ internal void InternalOnNetworkPreDespawn()
847854
{
848855
Debug.LogException(e);
849856
}
857+
858+
// Let each NetworkVariableBase derived instance know that
859+
// all spawn related methods have been invoked.
860+
for (int i = 0; i < NetworkVariableFields.Count; i++)
861+
{
862+
NetworkVariableFields[i].OnPreDespawn();
863+
}
850864
}
851865

852866
internal void InternalOnNetworkDespawn()

com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkList.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ public NetworkList(IEnumerable<T> values = default,
5858
Dispose();
5959
}
6060

61+
internal override void OnSpawned()
62+
{
63+
// If we are dirty and have write permissions by the time the NetworkObject
64+
// is finished spawning (same frame), then go ahead and reset the dirty related
65+
// properties for NetworkList.
66+
if (IsDirty() && CanSend())
67+
{
68+
UpdateLastSentTime();
69+
ResetDirty();
70+
SetDirty(false);
71+
}
72+
base.OnSpawned();
73+
}
74+
6175
/// <inheritdoc cref="NetworkVariable{T}.ResetDirty"/>
6276
public override void ResetDirty()
6377
{

com.unity.netcode.gameobjects/Runtime/NetworkVariable/NetworkVariableBase.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,28 @@ public void Initialize(NetworkBehaviour networkBehaviour)
140140
}
141141
}
142142

143+
/// TODO-API: After further vetting and alignment on these, we might make them part of the public API.
144+
/// Could actually be like an interface that gets automatically registered for these kinds of notifications
145+
/// without having to be a NetworkBehaviour.
146+
#region OnSpawn and OnPreDespawn (ETC)
147+
148+
/// <summary>
149+
/// Invoked after the associated <see cref="NetworkBehaviour.OnNetworkPostSpawn"/> has been invoked.
150+
/// </summary>
151+
internal virtual void OnSpawned()
152+
{
153+
154+
}
155+
156+
/// <summary>
157+
/// Invoked after the associated <see cref="NetworkBehaviour.OnNetworkPreDespawn"/> has been invoked.
158+
/// </summary>
159+
internal virtual void OnPreDespawn()
160+
{
161+
162+
}
163+
#endregion
164+
143165
/// <summary>
144166
/// Deinitialize is invoked when a NetworkObject is despawned.
145167
/// This allows for a recyled NetworkObject (in-scene or pooled)

0 commit comments

Comments
 (0)