OnClientDisconnect()

I’m trying to send an RPC to all clients when a single client disconnects (willingly, timed out, etc). The NetworkManager on the server should be able to track that but I can’t seem to figure out how to call for that trigger.

Before, all I had to do was type stuff like:

void OnPlayerDisconnected(NetworkPlayer player)
{
//Do stuff
}

I understand that (now) the server stuff is completely independent from the client stuff… which is, fine, I’ll call an RPC if I need the clients to register it but none of the things I’ve tried work. Can someone type what they’ve used (that works), please?

Thanks.

P.S. - Having documentation on both legacy and UNET is not helping… even stuff that aren’t labelled as “(legacy)” don’t seem to work. I haven’t found a single bit of info in the manual on how to deal with disconnects.

Player objects are automatically destroyed on the host and other clients when a client disconnects.

NetworkManager has this function that you can override, that is called on the server when a client disconnects:

        public virtual void OnServerDisconnect(NetworkConnection conn)
        {
            NetworkServer.DestroyPlayersForConnection(conn);
        }
2 Likes

public virtual void OnServerDisconnect(NetworkConnectionconn)
{
Debug.Log (“Something happened”);
}

It doesn’t work; neither clients nor server sends “Something happened” on the console. I just want clients to call a function when another disconnects.

You need to put override instead of virtual.

And then you can call an Rpc function from the server.

That works. Does it have to be overridden? I just want to add a function call to whatever else it’s doing.

Or is NetworkServer.DestroyPlayersForConnection(conn); all it does?

Well, you can always call the base function, like this:

public override void OnServerDisconnect(NetworkConnection conn)
{
    base.OnServerDisconnect(NetworkConnection conn);
    Debug.Log ("Something happened");
}

Thanks!

Actually, this is the only thing that doesn’t seem to work now. The server calls the Rpc function but the remaining clients don’t receive it for some reason.

public override void OnServerDisconnect(NetworkConnectionconn)
{
base.OnServerDisconnect(conn);

Debug.Log (“Something happened”);
Rpc_Step1();
}

[ClientRpc]
public void Rpc_Step1()
{
Debug.Log (“Step1”);
Step2 ();
}
public void Step2()
{
Debug.Log (“Step2”);
}

Server’s console read:
Step1
Step2

but none of the clients get anything.

Ah, I think I just figured it out myself.

You can only send Rpc’s to objects with NetworkBehaviour. This means that you can’t Rpc in the NetworkManager. You need a seperate object with the NetworkBehaviour where you can then call the Rpc function. Remember to also add a NetworkIdentity to that object.

The object also needs to be spawned by the server. So you should make it a prefab, and use the Spawn function when you create the server. It will automatically be spawned on the clients too then.

1 Like

That’s strange, I have a Cmd and Rpc function that work just fine in my NetworkManager.

BUT, transferring Rpc_Step1() over to a NetworkIdentity object did the trick.

You the man, Bmandk! Also thanks seanr for the assist :smile:

1 Like