Skip to content

feat: Adding "previousValue" in NetworkListEvent #1528

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 2 commits into from
Dec 15, 2021
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
1 change: 1 addition & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
## [Unreleased]

### Added
- Adding `PreviousValue` in NetworkListEvent, when `Value` has changed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:(

- - Adding `PreviousValue` in NetworkListEvent, when `Value` has changed
+ - Adding `PreviousValue` in NetworkListEvent, when `Value` has changed (#1528)


### Removed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,18 +272,22 @@ public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
{
reader.ReadValueSafe(out int index);
reader.ReadValueSafe(out T value);
if (index < m_List.Length)
if (index >= m_List.Length)
{
m_List[index] = value;
throw new Exception("Shouldn't be here, index is higher than list length");
}

var previousValue = m_List[index];
m_List[index] = value;

if (OnListChanged != null)
{
OnListChanged(new NetworkListEvent<T>
{
Type = eventType,
Index = index,
Value = value
Value = value,
PreviousValue = previousValue
});
}

Expand All @@ -293,7 +297,8 @@ public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
{
Type = eventType,
Index = index,
Value = value
Value = value,
PreviousValue = previousValue
});
}
}
Expand Down Expand Up @@ -528,6 +533,11 @@ public enum EventType : byte
/// </summary>
public T Value;

/// <summary>
/// The previous value when "Value" has changed, if available.
/// </summary>
public T PreviousValue;

/// <summary>
/// the index changed, added or removed if available
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,58 @@ public IEnumerator NetworkListArrayOperator([Values(true, false)] bool useHost)
);
}


[UnityTest]
public IEnumerator NetworkListValueUpdate([Values(true, false)] bool useHost)
{
m_TestWithHost = useHost;
yield return MultiInstanceHelpers.RunAndWaitForCondition(
() =>
{
m_Player1OnServer.TheList.Add(k_TestVal1);
},
() =>
{
return m_Player1OnServer.TheList.Count == 1 &&
m_Player1OnClient1.TheList.Count == 1 &&
m_Player1OnServer.TheList[0] == k_TestVal1 &&
m_Player1OnClient1.TheList[0] == k_TestVal1;
}
);

var testSucceeded = false;

void TestValueUpdatedCallback(NetworkListEvent<int> changedEvent)
{
testSucceeded = changedEvent.PreviousValue == k_TestVal1 &&
changedEvent.Value == k_TestVal3;
}

try
{
yield return MultiInstanceHelpers.RunAndWaitForCondition(
() =>
{
m_Player1OnServer.TheList[0] = k_TestVal3;
m_Player1OnClient1.TheList.OnListChanged += TestValueUpdatedCallback;
},
() =>
{
return m_Player1OnServer.TheList.Count == 1 &&
m_Player1OnClient1.TheList.Count == 1 &&
m_Player1OnServer.TheList[0] == k_TestVal3 &&
m_Player1OnClient1.TheList[0] == k_TestVal3;
}
);
}
finally
{
m_Player1OnClient1.TheList.OnListChanged -= TestValueUpdatedCallback;
}

Assert.That(testSucceeded);
}

[Test]
public void NetworkListIEnumerator([Values(true, false)] bool useHost)
{
Expand Down