Skip to content

fix: correctly setting PreviousValue when changing the element of NetworkList #2069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Additional documentation and release notes are available at [Multiplayer Documen

- 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)

- Fixed NetworkList Value event on the server. PreviousValue is now set correctly when a new value is set through property setter. (#2067)

## [1.0.0] - 2022-06-27

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,13 +452,15 @@ public T this[int index]
get => m_List[index];
set
{
var previousValue = m_List[index];
m_List[index] = value;

var listEvent = new NetworkListEvent<T>()
{
Type = NetworkListEvent<T>.EventType.Value,
Index = index,
Value = value
Value = value,
PreviousValue = previousValue
};

HandleAddListEvent(listEvent);
Expand Down
82 changes: 82 additions & 0 deletions com.unity.netcode.gameobjects/Tests/Runtime/ListChangedTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System.Collections;
using UnityEngine;
using UnityEngine.TestTools;
using Unity.Netcode.TestHelpers.Runtime;

namespace Unity.Netcode.RuntimeTests
{
public class NetworkListChangedTestComponent : NetworkBehaviour
{

}

public class ListChangedObject : NetworkBehaviour
{
public int ExpectedPreviousValue = 0;
public int ExpectedValue = 0;
public bool AddDone = false;

public NetworkList<int> MyNetworkList = new NetworkList<int>();

public override void OnNetworkSpawn()
{
MyNetworkList.OnListChanged += Changed;
base.OnNetworkSpawn();
}

public void Changed(NetworkListEvent<int> listEvent)
{
if (listEvent.Type == NetworkListEvent<int>.EventType.Value)
{
if (listEvent.PreviousValue != ExpectedPreviousValue)
{
Debug.Log($"Expected previous value mismatch {listEvent.PreviousValue} versus {ExpectedPreviousValue}");
Debug.Assert(listEvent.PreviousValue == ExpectedPreviousValue);
}

if (listEvent.Value != ExpectedValue)
{
Debug.Log($"Expected value mismatch {listEvent.Value} versus {ExpectedValue}");
Debug.Assert(listEvent.Value == ExpectedValue);
}

AddDone = true;
}
}
}

public class NetworkListChangedTests : NetcodeIntegrationTest
{
protected override int NumberOfClients => 2;

private ulong m_ClientId0;
private GameObject m_PrefabToSpawn;

private NetworkObject m_NetSpawnedObject1;

protected override void OnServerAndClientsCreated()
{
m_PrefabToSpawn = CreateNetworkObjectPrefab("ListChangedObject");
m_PrefabToSpawn.AddComponent<ListChangedObject>();
}

[UnityTest]
public IEnumerator NetworkListChangedTest()
{
m_ClientId0 = m_ClientNetworkManagers[0].LocalClientId;

// create 3 objects
var spawnedObject1 = SpawnObject(m_PrefabToSpawn, m_ServerNetworkManager);
m_NetSpawnedObject1 = spawnedObject1.GetComponent<NetworkObject>();

m_NetSpawnedObject1.GetComponent<ListChangedObject>().MyNetworkList.Add(42);
m_NetSpawnedObject1.GetComponent<ListChangedObject>().ExpectedPreviousValue = 42;
m_NetSpawnedObject1.GetComponent<ListChangedObject>().ExpectedValue = 44;
m_NetSpawnedObject1.GetComponent<ListChangedObject>().MyNetworkList[0] = 44;

Debug.Assert(m_NetSpawnedObject1.GetComponent<ListChangedObject>().AddDone);

return null;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.