Skip to content

Commit 2742346

Browse files
feat: Adding "previousValue" in NetworkListEvent (#1528)
* Quality of life, adding "previousValue" field in list events. This way, instead of users having to maintain a local value themselves, we just add it when individual values changes. It'll stay to default with other event types (just like "index" stays to default when the event type doesn't have an index) * update to changelog
1 parent 1c7b164 commit 2742346

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
99
## [Unreleased]
1010

1111
### Added
12+
- Adding `PreviousValue` in NetworkListEvent, when `Value` has changed
1213

1314
### Removed
1415

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
@@ -364,6 +364,58 @@ public IEnumerator NetworkListArrayOperator([Values(true, false)] bool useHost)
364364
);
365365
}
366366

367+
368+
[UnityTest]
369+
public IEnumerator NetworkListValueUpdate([Values(true, false)] bool useHost)
370+
{
371+
m_TestWithHost = useHost;
372+
yield return MultiInstanceHelpers.RunAndWaitForCondition(
373+
() =>
374+
{
375+
m_Player1OnServer.TheList.Add(k_TestVal1);
376+
},
377+
() =>
378+
{
379+
return m_Player1OnServer.TheList.Count == 1 &&
380+
m_Player1OnClient1.TheList.Count == 1 &&
381+
m_Player1OnServer.TheList[0] == k_TestVal1 &&
382+
m_Player1OnClient1.TheList[0] == k_TestVal1;
383+
}
384+
);
385+
386+
var testSucceeded = false;
387+
388+
void TestValueUpdatedCallback(NetworkListEvent<int> changedEvent)
389+
{
390+
testSucceeded = changedEvent.PreviousValue == k_TestVal1 &&
391+
changedEvent.Value == k_TestVal3;
392+
}
393+
394+
try
395+
{
396+
yield return MultiInstanceHelpers.RunAndWaitForCondition(
397+
() =>
398+
{
399+
m_Player1OnServer.TheList[0] = k_TestVal3;
400+
m_Player1OnClient1.TheList.OnListChanged += TestValueUpdatedCallback;
401+
},
402+
() =>
403+
{
404+
return m_Player1OnServer.TheList.Count == 1 &&
405+
m_Player1OnClient1.TheList.Count == 1 &&
406+
m_Player1OnServer.TheList[0] == k_TestVal3 &&
407+
m_Player1OnClient1.TheList[0] == k_TestVal3;
408+
}
409+
);
410+
}
411+
finally
412+
{
413+
m_Player1OnClient1.TheList.OnListChanged -= TestValueUpdatedCallback;
414+
}
415+
416+
Assert.That(testSucceeded);
417+
}
418+
367419
[Test]
368420
public void NetworkListIEnumerator([Values(true, false)] bool useHost)
369421
{

0 commit comments

Comments
 (0)