Skip to content

Commit eaac2e5

Browse files
authored
fix: correctly setting PreviousValue when changing the element of NetworkList (#2069)
1 parent 1584749 commit eaac2e5

File tree

4 files changed

+98
-1
lines changed

4 files changed

+98
-1
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Additional documentation and release notes are available at [Multiplayer Documen
1414

1515
- Fixed NetworkLists not populating on client. NetworkList now uses the most recent list as opposed to the list at the end of previous frame, when sending full updates to dynamically spawned NetworkObject. The difference in behaviour is required as scene management spawns those objects at a different time in the frame, relative to updates. (#2062)
1616

17+
- Fixed NetworkList Value event on the server. PreviousValue is now set correctly when a new value is set through property setter. (#2067)
18+
1719
## [1.0.0] - 2022-06-27
1820

1921
### Changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,13 +452,15 @@ public T this[int index]
452452
get => m_List[index];
453453
set
454454
{
455+
var previousValue = m_List[index];
455456
m_List[index] = value;
456457

457458
var listEvent = new NetworkListEvent<T>()
458459
{
459460
Type = NetworkListEvent<T>.EventType.Value,
460461
Index = index,
461-
Value = value
462+
Value = value,
463+
PreviousValue = previousValue
462464
};
463465

464466
HandleAddListEvent(listEvent);
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System.Collections;
2+
using UnityEngine;
3+
using UnityEngine.TestTools;
4+
using Unity.Netcode.TestHelpers.Runtime;
5+
6+
namespace Unity.Netcode.RuntimeTests
7+
{
8+
public class NetworkListChangedTestComponent : NetworkBehaviour
9+
{
10+
11+
}
12+
13+
public class ListChangedObject : NetworkBehaviour
14+
{
15+
public int ExpectedPreviousValue = 0;
16+
public int ExpectedValue = 0;
17+
public bool AddDone = false;
18+
19+
public NetworkList<int> MyNetworkList = new NetworkList<int>();
20+
21+
public override void OnNetworkSpawn()
22+
{
23+
MyNetworkList.OnListChanged += Changed;
24+
base.OnNetworkSpawn();
25+
}
26+
27+
public void Changed(NetworkListEvent<int> listEvent)
28+
{
29+
if (listEvent.Type == NetworkListEvent<int>.EventType.Value)
30+
{
31+
if (listEvent.PreviousValue != ExpectedPreviousValue)
32+
{
33+
Debug.Log($"Expected previous value mismatch {listEvent.PreviousValue} versus {ExpectedPreviousValue}");
34+
Debug.Assert(listEvent.PreviousValue == ExpectedPreviousValue);
35+
}
36+
37+
if (listEvent.Value != ExpectedValue)
38+
{
39+
Debug.Log($"Expected value mismatch {listEvent.Value} versus {ExpectedValue}");
40+
Debug.Assert(listEvent.Value == ExpectedValue);
41+
}
42+
43+
AddDone = true;
44+
}
45+
}
46+
}
47+
48+
public class NetworkListChangedTests : NetcodeIntegrationTest
49+
{
50+
protected override int NumberOfClients => 2;
51+
52+
private ulong m_ClientId0;
53+
private GameObject m_PrefabToSpawn;
54+
55+
private NetworkObject m_NetSpawnedObject1;
56+
57+
protected override void OnServerAndClientsCreated()
58+
{
59+
m_PrefabToSpawn = CreateNetworkObjectPrefab("ListChangedObject");
60+
m_PrefabToSpawn.AddComponent<ListChangedObject>();
61+
}
62+
63+
[UnityTest]
64+
public IEnumerator NetworkListChangedTest()
65+
{
66+
m_ClientId0 = m_ClientNetworkManagers[0].LocalClientId;
67+
68+
// create 3 objects
69+
var spawnedObject1 = SpawnObject(m_PrefabToSpawn, m_ServerNetworkManager);
70+
m_NetSpawnedObject1 = spawnedObject1.GetComponent<NetworkObject>();
71+
72+
m_NetSpawnedObject1.GetComponent<ListChangedObject>().MyNetworkList.Add(42);
73+
m_NetSpawnedObject1.GetComponent<ListChangedObject>().ExpectedPreviousValue = 42;
74+
m_NetSpawnedObject1.GetComponent<ListChangedObject>().ExpectedValue = 44;
75+
m_NetSpawnedObject1.GetComponent<ListChangedObject>().MyNetworkList[0] = 44;
76+
77+
Debug.Assert(m_NetSpawnedObject1.GetComponent<ListChangedObject>().AddDone);
78+
79+
return null;
80+
}
81+
}
82+
}

com.unity.netcode.gameobjects/Tests/Runtime/ListChangedTest.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)