Skip to content

Commit e6f4bf2

Browse files
update
Based on peer review, folding the same logic for spawn authority resetting dirty once spawned to assure no duplicate changes are sent.
1 parent 7fd7631 commit e6f4bf2

File tree

4 files changed

+21
-58
lines changed

4 files changed

+21
-58
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ internal void NetworkPostSpawn()
838838
// all spawn related methods have been invoked.
839839
for (int i = 0; i < NetworkVariableFields.Count; i++)
840840
{
841-
NetworkVariableFields[i].OnSpawned();
841+
NetworkVariableFields[i].InternalOnSpawned();
842842
}
843843
}
844844

@@ -891,7 +891,7 @@ internal void InternalOnNetworkPreDespawn()
891891
// all spawn related methods have been invoked.
892892
for (int i = 0; i < NetworkVariableFields.Count; i++)
893893
{
894-
NetworkVariableFields[i].OnPreDespawn();
894+
NetworkVariableFields[i].InternalOnPreDespawn();
895895
}
896896
}
897897

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

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

61-
internal override void OnSpawned()
62-
{
63-
// If the NetworkList is:
64-
// - On the spawn authority side.
65-
// - Dirty
66-
// - State updates can be sent:
67-
// -- The instance has write permissions.
68-
// -- The last sent time plus the max send time period is less than the current time.
69-
// - User script has modified the list during spawn.
70-
// When the NetworkObject is finished spawning (on the same frame), go ahead and reset
71-
// the dirty related properties and last sent time to prevent duplicate entries from
72-
// being sent (i.e. CreateObjectMessage will contain the changes so we don't need to
73-
// send a proceeding NetworkVariableDeltaMessage).
74-
if (m_NetworkObject.IsSpawnAuthority && IsDirty() && CanWrite() && CanSend())
75-
{
76-
UpdateLastSentTime();
77-
ResetDirty();
78-
SetDirty(false);
79-
}
80-
base.OnSpawned();
81-
}
82-
8361
/// <inheritdoc cref="NetworkVariable{T}.ResetDirty"/>
8462
public override void ResetDirty()
8563
{

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

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -407,30 +407,5 @@ internal override void WriteFieldSynchronization(FastBufferWriter writer)
407407
base.WriteFieldSynchronization(writer);
408408
}
409409
}
410-
411-
/// <summary>
412-
/// Notification we have fully spawned the associated <see cref="NetworkObject"/>.
413-
/// </summary>
414-
internal override void OnSpawned()
415-
{
416-
// If the NetworkVariable is:
417-
// - On the spawn authority side.
418-
// - Dirty.
419-
// - State updates can be sent:
420-
// -- The instance has write permissions.
421-
// -- The last sent time plus the max send time period is less than the current time.
422-
// - User script has modified the list during spawn.
423-
// When the NetworkObject is finished spawning (on the same frame), go ahead and reset
424-
// the dirty related properties and last sent time to prevent duplicate updates from
425-
// being sent (i.e. CreateObjectMessage will contain the changes so we don't need to
426-
// send a proceeding NetworkVariableDeltaMessage).
427-
if (m_NetworkObject.IsSpawnAuthority && IsDirty() && CanWrite() && CanSend())
428-
{
429-
UpdateLastSentTime();
430-
ResetDirty();
431-
SetDirty(false);
432-
}
433-
base.OnSpawned();
434-
}
435410
}
436411
}

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,27 +140,37 @@ 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-
148143
/// <summary>
149144
/// Invoked after the associated <see cref="NetworkBehaviour.OnNetworkPostSpawn"/> has been invoked.
150145
/// </summary>
151-
internal virtual void OnSpawned()
146+
internal void InternalOnSpawned()
152147
{
153-
148+
// If the NetworkVariableBase derived class is:
149+
// - On the spawn authority side.
150+
// - Dirty.
151+
// - State updates can be sent:
152+
// -- The instance has write permissions.
153+
// -- The last sent time plus the max send time period is less than the current time.
154+
// - User script has modified the list during spawn.
155+
// When the NetworkObject is finished spawning (on the same frame), go ahead and reset
156+
// the dirty related properties and last sent time to prevent duplicate updates from
157+
// being sent (i.e. CreateObjectMessage will contain the changes so we don't need to
158+
// send a proceeding NetworkVariableDeltaMessage).
159+
if (m_NetworkObject.IsSpawnAuthority && IsDirty() && CanWrite() && CanSend())
160+
{
161+
UpdateLastSentTime();
162+
ResetDirty();
163+
SetDirty(false);
164+
}
154165
}
155166

156167
/// <summary>
157168
/// Invoked after the associated <see cref="NetworkBehaviour.OnNetworkPreDespawn"/> has been invoked.
158169
/// </summary>
159-
internal virtual void OnPreDespawn()
170+
internal void InternalOnPreDespawn()
160171
{
161172

162173
}
163-
#endregion
164174

165175
/// <summary>
166176
/// Deinitialize is invoked when a NetworkObject is despawned.

0 commit comments

Comments
 (0)