Skip to content

Commit 4be8457

Browse files
feat: Adding "previousValue" in NetworkListEvent (#1528) (#1577)
1 parent b14204c commit 4be8457

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
66

77
Additional documentation and release notes are available at [Multiplayer Documentation](https://docs-multiplayer.unity3d.com).
88

9+
## [Unreleased]
10+
11+
### Added
12+
13+
- Added `PreviousValue` in NetworkListEvent, when `Value` has changed (#1528)
14+
915
## [1.0.0-pre.4] - 2021-01-04
1016

1117
### Added

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,18 +272,22 @@ public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
272272
{
273273
reader.ReadValueSafe(out int index);
274274
reader.ReadValueSafe(out T value);
275-
if (index < m_List.Length)
275+
if (index >= m_List.Length)
276276
{
277-
m_List[index] = value;
277+
throw new Exception("Shouldn't be here, index is higher than list length");
278278
}
279279

280+
var previousValue = m_List[index];
281+
m_List[index] = value;
282+
280283
if (OnListChanged != null)
281284
{
282285
OnListChanged(new NetworkListEvent<T>
283286
{
284287
Type = eventType,
285288
Index = index,
286-
Value = value
289+
Value = value,
290+
PreviousValue = previousValue
287291
});
288292
}
289293

@@ -293,7 +297,8 @@ public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
293297
{
294298
Type = eventType,
295299
Index = index,
296-
Value = value
300+
Value = value,
301+
PreviousValue = previousValue
297302
});
298303
}
299304
}
@@ -528,6 +533,11 @@ public enum EventType : byte
528533
/// </summary>
529534
public T Value;
530535

536+
/// <summary>
537+
/// The previous value when "Value" has changed, if available.
538+
/// </summary>
539+
public T PreviousValue;
540+
531541
/// <summary>
532542
/// the index changed, added or removed if available
533543
/// </summary>

com.unity.netcode.gameobjects/Tests/Runtime/NetworkVariableTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,58 @@ public IEnumerator NetworkListArrayOperator([Values(true, false)] bool useHost)
328328
);
329329
}
330330

331+
332+
[UnityTest]
333+
public IEnumerator NetworkListValueUpdate([Values(true, false)] bool useHost)
334+
{
335+
m_TestWithHost = useHost;
336+
yield return MultiInstanceHelpers.RunAndWaitForCondition(
337+
() =>
338+
{
339+
m_Player1OnServer.TheList.Add(k_TestVal1);
340+
},
341+
() =>
342+
{
343+
return m_Player1OnServer.TheList.Count == 1 &&
344+
m_Player1OnClient1.TheList.Count == 1 &&
345+
m_Player1OnServer.TheList[0] == k_TestVal1 &&
346+
m_Player1OnClient1.TheList[0] == k_TestVal1;
347+
}
348+
);
349+
350+
var testSucceeded = false;
351+
352+
void TestValueUpdatedCallback(NetworkListEvent<int> changedEvent)
353+
{
354+
testSucceeded = changedEvent.PreviousValue == k_TestVal1 &&
355+
changedEvent.Value == k_TestVal3;
356+
}
357+
358+
try
359+
{
360+
yield return MultiInstanceHelpers.RunAndWaitForCondition(
361+
() =>
362+
{
363+
m_Player1OnServer.TheList[0] = k_TestVal3;
364+
m_Player1OnClient1.TheList.OnListChanged += TestValueUpdatedCallback;
365+
},
366+
() =>
367+
{
368+
return m_Player1OnServer.TheList.Count == 1 &&
369+
m_Player1OnClient1.TheList.Count == 1 &&
370+
m_Player1OnServer.TheList[0] == k_TestVal3 &&
371+
m_Player1OnClient1.TheList[0] == k_TestVal3;
372+
}
373+
);
374+
}
375+
finally
376+
{
377+
m_Player1OnClient1.TheList.OnListChanged -= TestValueUpdatedCallback;
378+
}
379+
380+
Assert.That(testSucceeded);
381+
}
382+
331383
[Test]
332384
public void NetworkListIEnumerator([Values(true, false)] bool useHost)
333385
{

0 commit comments

Comments
 (0)