Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question ServerRpc not running after being called by Client

Discussion in 'Netcode for GameObjects' started by GuirieSanchez, Nov 12, 2022.

  1. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    218
    I have a simple setup:
    (1) a NetworkManager that spawns a "PlayerObject"
    (2) the "PlayerObject" itself being spawned after each client or the host connects
    (3) a "NetManagerScript" (with NetworkBehavior) with a "Network Object" component attached to it.

    In the (3) NetManagerScript, I have all the [ServerRpc]'s and [ClientRpc]'s setup, and all other scripts, including the (2) "PlayerObject" script, have a reference of it.
    So far, I've been running [ServerRpc]'s and [ClientRpc]'s successfully, but I came across a case where one of the [ServerRpc] is not running when being called from a client. I'll give the details of it:

    - The object calling the ServerRpc is the "PlayerObject" when it gets despawned. I use the following code to call the ServerRpc:

    Code (CSharp):
    1. public override void OnNetworkDespawn()
    2.     {
    3.         base.OnNetworkDespawn();
    4.  
    5.         if (!IsOwner) return;
    6.         Debug.LogWarning($"OwnerClientId({OwnerClientId}) is disconnecting and giving back its ID");
    7.         NetManagerScript.ReturningNetworkIDToList_ServerRpc(networkPlayerID);
    8.     }
    - The [ServerRpc] is being called on the "NetManagerScript" only when the Server or Host gets disconnected. The code is the following:

    Code (CSharp):
    1. [ServerRpc(RequireOwnership = false)]
    2.         public void ReturningNetworkIDToList_ServerRpc(int networkID)
    3.         {
    4.  
    5.             ListOfIDs.Add(networkID);
    6.             Debug.Log("RETURNING ID");
    7.  
    8.         }
    So, as I mentioned, the
    Debug.Log
    and the code inside only get triggered if the host gets disconnected, but I have no clue why it's not working when a client gets disconnected too.

    PS: I don't use a [ClientRpc] following the ServerRpc because I only want the code inside to be applied on the server/host whenever any of the connected clients get disconnected.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    1,616
    just a guess but maybe OnNetworkDespawn is "too late" to be sending any more RPC messages from that object
    it would work on the host because the host doesn't actually send that RPC over the network, it's a direct method call
     
    GuirieSanchez likes this.
  3. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    218
    Oh, I didn't know the host doesn't send RPCs over the network... I thought it should work since it was working on the host. Anyway, that makes sense. Hmmm...

    The reason why I used "OnNetworkDespawn()" is because in NGO I couldn't find any callbacks for "OnClientDisconnect", as opposed to UNET or other tools.

    Is there any known way to check this?
     
  4. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    218
    Ok, actually there's an "OnClientDisconnected" callback. It works, but not for my purposes. The client has some data I want to get before they disconnect. The "OnClientDisconnected" callback gets triggered always on the Host, there's no way for me to know who got disconnected and therefore get the data.

    By default, I get the data from the Host every single time when using this callback.
     
    Last edited: Nov 13, 2022