Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved ServerRPC Behavior when Using Coroutines.

Discussion in 'Netcode for GameObjects' started by Sushi8819, Oct 31, 2023.

  1. Sushi8819

    Sushi8819

    Joined:
    Oct 20, 2023
    Posts:
    17
    I want to perform an HTTP request from a ServerRpc and store the result in a NetworkList.
    The code is simplified as follows.
    The issue is that the NetworkList is updated when the ServerRpc is called for the second time. This means that the result of the first call is updated at the time of the second ServerRpc call, and the result of the second call is updated at the time of the third ServerRpc call, and so on.
    The same behavior is observed with NetworkList's OnValueChanged event.
    If anyone knows the cause of this, I would appreciate your help.

    Code (CSharp):
    1.  
    2. public NetworkList<UserInfo> users;
    3.  
    4. OnEnable(){
    5.     users.OnListChanged += NetworkListChangedListener;
    6. }
    7.  
    8. private void NetworkListChangedListener(NetworkListEvent<UserInfo> changeEvent)
    9.     {
    10.     Debug.Log("NetworkList is changed.")
    11.     }
    12.  
    13. [ServerRpc(RequireOwnership = false)]
    14. private void SetUserInfoServerRPC(string id, ServerRpcParams serverRpcParams = default)
    15.     {
    16.         Debug.Log("ServerRPC");
    17.         StartCoroutine(GetUserInfo(id, serverRpcParams.Receive.SenderClientId));
    18.     }
    19.  
    20. private IEnumerator GetUserInfo(string uToken, ulong clientid)
    21. {
    22.     ....
    23.     yield return request.SendWebRequest();
    24.     ....
    25.  
    26.     UserInfo userinfo = new UserInfo(infoA, infoB, infoC);
    27.     users.Add(userinfo);
    28. }