🐾PurrNet Docs
Sync Type

SyncList

SyncList<T> is a networked list that automatically propagates add, remove, insert, and clear operations to all clients. Changes are delta-synced — only the operation is sent, not the full list.

How it works

SyncList<T> behaves like a regular List<T>, but every mutation (Add, Remove, Insert, Clear, index assignment) is replicated to all observers. Each operation fires the onChanged callback with a SyncListChange<T> struct describing what changed.

Late-joining clients receive the full current list state on connect.

InventorySystem.cs
using PurrNet;
using System.Collections.Generic;

public class InventorySystem : NetworkBehaviour
{
    // ownerAuth: only the owner can modify their inventory
    private SyncList<int> _items = new SyncList<int>(ownerAuth: true);

    private void Awake()
    {
        _items.onChanged += OnInventoryChanged;
    }

    private void OnInventoryChanged(SyncListChange<int> change)
    {
        switch (change.operation)
        {
            case SyncListOperation.Added:
                UI.ShowPickupNotification(change.value);
                break;
            case SyncListOperation.Removed:
                UI.ShowDropNotification(change.value);
                break;
            case SyncListOperation.Cleared:
                UI.RefreshInventoryGrid(_items);
                break;
        }
    }

    // Owner can call this — ownerAuth:true means server allows it
    [ServerRpc(requireOwnership: true)]
    public void CmdPickupItem(int itemId)
    {
        if (_items.Count >= maxItems) return;
        _items.Add(itemId);
    }

    [ServerRpc(requireOwnership: true)]
    public void CmdDropItem(int itemId)
    {
        _items.Remove(itemId);
    }
}

SyncListChange properties

ParameterTypeDefaultDescription
.operationSyncListOperation—The type of change: Added, Removed, Insert, Set, or Cleared.
.valueT—The new value associated with the change.
.indexint—The affected list index position.

Available methods

  • Add(T item) — appends to the end
  • Remove(T item) — removes the first occurrence by value
  • RemoveAt(int index) — removes by index
  • Insert(int index, T item) — inserts at position
  • Clear() — removes all elements
  • list[index] = value — updates value at index (fires Set operation)
  • SetDirty(int index) — forces re-sync of an element (useful for mutable structs)
  • Count, Contains(), IndexOf() — standard read operations

Situational example — team roster

TeamManager.cs
public class TeamManager : NetworkBehaviour
{
    private SyncList<PlayerID> _redTeam  = new();
    private SyncList<PlayerID> _blueTeam = new();

    private void Awake()
    {
        _redTeam.onChanged  += _ => UpdateTeamUI("Red",  _redTeam);
        _blueTeam.onChanged += _ => UpdateTeamUI("Blue", _blueTeam);
    }

    protected override void OnSpawned(bool asServer)
    {
        if (!asServer) return;

        // Subscribe to player join/leave events
        networkManager.onPlayerConnected    += OnPlayerJoined;
        networkManager.onPlayerDisconnected += OnPlayerLeft;
    }

    private void OnPlayerJoined(PlayerID player)
    {
        // Assign to smaller team
        if (_redTeam.Count <= _blueTeam.Count)
            _redTeam.Add(player);
        else
            _blueTeam.Add(player);
    }

    private void OnPlayerLeft(PlayerID player)
    {
        _redTeam.Remove(player);
        _blueTeam.Remove(player);
    }
}

SetDirty for mutable structs

If T is a struct and you mutate a field of an element in place (e.g. _list[0].health--), SyncList cannot detect the change automatically. Call _list.SetDirty(0) after the mutation to force a sync.