Skip to content

Networklist OnListChanged does not work for clients #2689

@patriksima

Description

@patriksima

Description

NetworkList OnListChanged event is called only on host/server. How can the client be notified of changes to the list?

Reproduce Steps

Basic scene is here, including simple UI (start host, client, change data)
https://github.com/patriksima/unity-networklist-onlistchanged-bug


public class NetworkListTest : NetworkBehaviour
{
    public NetworkList<int> IntList;

    private void Awake()
    {
        IntList = new NetworkList<int>(new[] { 1, 2, 3, 4, 5 });
        NetworkLog.LogInfoServer("Awake");
    }

    public override void OnNetworkSpawn()
    {
        base.OnNetworkSpawn();
        if (IsClient)
        {
            IntList.OnListChanged += IntListChanged;
        }

        NetworkLog.LogInfoServer("OnNetworkSpawn");
    }

    public override void OnNetworkDespawn()
    {
        if (IsClient)
        {
            IntList.OnListChanged -= IntListChanged;
        }

        base.OnNetworkDespawn();
        NetworkLog.LogInfoServer("OnNetworkDespawn");
    }

    public override void OnDestroy()
    {
        IntList.ResetDirty();
        base.OnDestroy();
        Debug.Log("OnDestroy");
    }

    private void IntListChanged(NetworkListEvent<int> changeEvent)
    {
        if (IsClient)
            Debug.Log("IntListChanged on client");
        NetworkLog.LogInfoServer(
            $"IntListChanged: IntList has changed by event '{changeEvent.Type}' with value '{changeEvent.Value}'");
    }

    [ServerRpc(RequireOwnership = false)]
    public void ChangeListDataServerRpc()
    {
        IntList[0] = 0;
        IntList.Add(10);
        IntList.Remove(1);
        IntList.SetDirty(true);

        NetworkLog.LogInfoServer("ChangeListDataServerRpc");

        CallAllClientsClientRpc();
    }

    [ClientRpc]
    public void CallAllClientsClientRpc()
    {
       // We're trying to force an immediate change to the list
        NetworkLog.LogInfoServer("CallAllClientsClientRpc");
    }

    public void OnChangeListData()
    {
        ChangeListDataServerRpc();
    }
}


Expected Outcome

As soon as the networklist is synced on the client, the OnListChanged should be fired.

Environment

  • OS: Windows 11 Home, ver 22H2
  • Unity Version: 2022.3.0f1
  • Netcode Version: 1.6.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions