Skip to content

Commit 24388c4

Browse files
authored
fix: NetworkList serialization sends the list values before the cached updates, to prevent duplicate application (#1917)
1 parent 2de6754 commit 24388c4

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

  • com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace Unity.Netcode
1111
public class NetworkList<T> : NetworkVariableSerialization<T> where T : unmanaged, IEquatable<T>
1212
{
1313
private NativeList<T> m_List = new NativeList<T>(64, Allocator.Persistent);
14+
private NativeList<T> m_ListAtLastReset = new NativeList<T>(64, Allocator.Persistent);
1415
private NativeList<NetworkListEvent<T>> m_DirtyEvents = new NativeList<NetworkListEvent<T>>(64, Allocator.Persistent);
1516

1617
/// <summary>
@@ -41,7 +42,11 @@ public NetworkList(IEnumerable<T> values = default,
4142
public override void ResetDirty()
4243
{
4344
base.ResetDirty();
44-
m_DirtyEvents.Clear();
45+
if (m_DirtyEvents.Length > 0)
46+
{
47+
m_DirtyEvents.Clear();
48+
m_ListAtLastReset.CopyFrom(m_List);
49+
}
4550
}
4651

4752
/// <inheritdoc />
@@ -109,10 +114,10 @@ public override void WriteDelta(FastBufferWriter writer)
109114
/// <inheritdoc />
110115
public override void WriteField(FastBufferWriter writer)
111116
{
112-
writer.WriteValueSafe((ushort)m_List.Length);
113-
for (int i = 0; i < m_List.Length; i++)
117+
writer.WriteValueSafe((ushort)m_ListAtLastReset.Length);
118+
for (int i = 0; i < m_ListAtLastReset.Length; i++)
114119
{
115-
Write(writer, m_List[i]);
120+
Write(writer, m_ListAtLastReset[i]);
116121
}
117122
}
118123

@@ -454,6 +459,7 @@ public int LastModifiedTick
454459
public override void Dispose()
455460
{
456461
m_List.Dispose();
462+
m_ListAtLastReset.Dispose();
457463
m_DirtyEvents.Dispose();
458464
}
459465
}

0 commit comments

Comments
 (0)