Skip to content
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 @@ -11,6 +11,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
### Added

- Clicking on the Help icon in the inspector will now redirect to the relevant documentation. (#3663)
- 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)

### Changed

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Unity.Collections;

namespace Unity.Netcode
Expand Down Expand Up @@ -597,45 +598,60 @@ public void RemoveAt(int index)
}

/// <summary>
/// Gets or sets the element at the specified index in the <see cref="NetworkList{T}"/>.
/// Sets the element at the specified index in the <see cref="NetworkList{T}"/>.
/// </summary>
/// <remarks>
/// This method checks for write permissions before setting the value.
/// This method checks for write permissions and equality before setting and updating the value.
/// </remarks>
/// <param name="index">The zero-based index of the element to get or set.</param>
/// <value>The element at the specified index.</value>
public T this[int index]
/// <param name="index">The zero-based index of the element to set.</param>
/// <param name="value">The new value to set at the given index</param>
/// <param name="forceUpdate">
/// Ignores the equality check when setting the value.
/// This option can send unnecessary updates to all clients when the value hasn't changed.
/// </param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Set(int index, T value, bool forceUpdate = false)
{
get => m_List[index];
set
// check write permissions
if (CannotWrite)
{
// check write permissions
if (CannotWrite)
{
LogWritePermissionError();
return;
}
LogWritePermissionError();
return;
}

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

// Only trigger an event if the value has changed
if (NetworkVariableSerialization<T>.AreEqual(ref previousValue, ref value))
{
return;
}
// Only trigger an event if the value has changed
if (!forceUpdate && NetworkVariableSerialization<T>.AreEqual(ref previousValue, ref value))
{
return;
}

m_List[index] = value;
m_List[index] = value;

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

HandleAddListEvent(listEvent);
}
HandleAddListEvent(listEvent);
}

/// <summary>
/// Gets or sets the element at the specified index in the <see cref="NetworkList{T}"/>.
/// </summary>
/// <remarks>
/// This method checks for write permissions before setting the value.
/// </remarks>
/// <param name="index">The zero-based index of the element to get or set.</param>
/// <value>The element at the specified index.</value>
public T this[int index]
{
get => m_List[index];
set => Set(index, value);
}

/// <summary>
Expand Down
Loading