Search Unity

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:
    452
    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:
    6,005
    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:
    452
    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:
    452
    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
  5. KOFKOF_

    KOFKOF_

    Joined:
    Jan 10, 2022
    Posts:
    1

    Any other idea to make the client "warn" the host he left the server?

    Thanks in advance.
     
  6. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,005
    Like I say, this is not possible after the fact (= once NetworkManager is shutting down).

    You can only force any "quit" functionality to send a ServerRpc and wait for the corresponding ClientRpc response before shutting down the NetworkManager (and don't forget programming a time out in case the ClientRpc never arrives).

    However, you will also have to intercept system-wide means of exiting, such as the user pressing Alt+F4 or clicking the window's X button. It is possible that in these instances, with the window/app already deep in the shutdown process Unity / NGO may already be shutting down when you receive that event.

    In general, there will ALWAYS be cases where a client disconnects randomly. Be it "force close" via Task Manager or pulling the plug of the machine/router.

    Consider if whatever info you need upon a client quitting couldn't instead be sent when the client connects again. What kind of information is this anyway?