|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
| 3 | +using System.Runtime.CompilerServices; |
3 | 4 | using Unity.Collections;
|
4 | 5 |
|
5 | 6 | namespace Unity.Netcode
|
@@ -597,45 +598,60 @@ public void RemoveAt(int index)
|
597 | 598 | }
|
598 | 599 |
|
599 | 600 | /// <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}"/>. |
601 | 602 | /// </summary>
|
602 | 603 | /// <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. |
604 | 605 | /// </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) |
608 | 614 | {
|
609 |
| - get => m_List[index]; |
610 |
| - set |
| 615 | + // check write permissions |
| 616 | + if (CannotWrite) |
611 | 617 | {
|
612 |
| - // check write permissions |
613 |
| - if (CannotWrite) |
614 |
| - { |
615 |
| - LogWritePermissionError(); |
616 |
| - return; |
617 |
| - } |
| 618 | + LogWritePermissionError(); |
| 619 | + return; |
| 620 | + } |
618 | 621 |
|
619 |
| - var previousValue = m_List[index]; |
| 622 | + var previousValue = m_List[index]; |
620 | 623 |
|
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 | + } |
626 | 629 |
|
627 |
| - m_List[index] = value; |
| 630 | + m_List[index] = value; |
628 | 631 |
|
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 | + }; |
636 | 639 |
|
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); |
639 | 655 | }
|
640 | 656 |
|
641 | 657 | /// <summary>
|
|
0 commit comments