Skip to content

Commit d68c986

Browse files
feat: Add NetworkList getter and redo NetworkList tests (#3690)
## Purpose of this PR Add setter onto `NetworkList` that allows an update to be "forced" even if the value was not changed. fixes: #3657 ### Jira ticket MTT-13366 ### Changelog - Added: A `Set` function onto the `NetworkList` that takes an optional parameter that will force the update to be processed and will ignore the equality check. ## Documentation - No documentation changes or additions were necessary. ## Testing & QA (How your changes can be verified during release Playtest) ### Functional Testing - [ ] `Manual testing done` _Automated tests:_ - [x] `Covered by existing automated tests` - [x] `Covered by new automated tests` _Does the change require QA team to:_ - [ ] `Review automated tests`? - [ ] `Execute manual tests`? - [ ] `Provide feedback about the PR`? If any boxes above are checked the QA team will be automatically added as a PR reviewer. ## Backports This is a 2.x only change --------- Co-authored-by: Noel Stephens <[email protected]>
1 parent 47ed2dd commit d68c986

File tree

6 files changed

+461
-408
lines changed

6 files changed

+461
-408
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1111
### Added
1212

1313
- Clicking on the Help icon in the inspector will now redirect to the relevant documentation. (#3663)
14+
- Added a `Set` function onto `NetworkList` that takes an optional parameter that forces an update to be processed even if the current value is equal to the previous value. (#3690)
1415

1516
### Changed
1617

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

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Runtime.CompilerServices;
34
using Unity.Collections;
45

56
namespace Unity.Netcode
@@ -597,45 +598,60 @@ public void RemoveAt(int index)
597598
}
598599

599600
/// <summary>
600-
/// Gets or sets the element at the specified index in the <see cref="NetworkList{T}"/>.
601+
/// Sets the element at the specified index in the <see cref="NetworkList{T}"/>.
601602
/// </summary>
602603
/// <remarks>
603-
/// This method checks for write permissions before setting the value.
604+
/// This method checks for write permissions and equality before setting and updating the value.
604605
/// </remarks>
605-
/// <param name="index">The zero-based index of the element to get or set.</param>
606-
/// <value>The element at the specified index.</value>
607-
public T this[int index]
606+
/// <param name="index">The zero-based index of the element to set.</param>
607+
/// <param name="value">The new value to set at the given index</param>
608+
/// <param name="forceUpdate">
609+
/// Ignores the equality check when setting the value.
610+
/// This option can send unnecessary updates to all clients when the value hasn't changed.
611+
/// </param>
612+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
613+
public void Set(int index, T value, bool forceUpdate = false)
608614
{
609-
get => m_List[index];
610-
set
615+
// check write permissions
616+
if (CannotWrite)
611617
{
612-
// check write permissions
613-
if (CannotWrite)
614-
{
615-
LogWritePermissionError();
616-
return;
617-
}
618+
LogWritePermissionError();
619+
return;
620+
}
618621

619-
var previousValue = m_List[index];
622+
var previousValue = m_List[index];
620623

621-
// Only trigger an event if the value has changed
622-
if (NetworkVariableSerialization<T>.AreEqual(ref previousValue, ref value))
623-
{
624-
return;
625-
}
624+
// Only trigger an event if the value has changed
625+
if (!forceUpdate && NetworkVariableSerialization<T>.AreEqual(ref previousValue, ref value))
626+
{
627+
return;
628+
}
626629

627-
m_List[index] = value;
630+
m_List[index] = value;
628631

629-
var listEvent = new NetworkListEvent<T>()
630-
{
631-
Type = NetworkListEvent<T>.EventType.Value,
632-
Index = index,
633-
Value = value,
634-
PreviousValue = previousValue
635-
};
632+
var listEvent = new NetworkListEvent<T>()
633+
{
634+
Type = NetworkListEvent<T>.EventType.Value,
635+
Index = index,
636+
Value = value,
637+
PreviousValue = previousValue
638+
};
636639

637-
HandleAddListEvent(listEvent);
638-
}
640+
HandleAddListEvent(listEvent);
641+
}
642+
643+
/// <summary>
644+
/// Gets or sets the element at the specified index in the <see cref="NetworkList{T}"/>.
645+
/// </summary>
646+
/// <remarks>
647+
/// This method checks for write permissions before setting the value.
648+
/// </remarks>
649+
/// <param name="index">The zero-based index of the element to get or set.</param>
650+
/// <value>The element at the specified index.</value>
651+
public T this[int index]
652+
{
653+
get => m_List[index];
654+
set => Set(index, value);
639655
}
640656

641657
/// <summary>

0 commit comments

Comments
 (0)