- Home /
Multiplayer - Handling players leaving
What is the proper way to handle a player leaving a server during a game match? Currently, I have the OnPlayerDisconnected method as follows.
void OnPlayerDisconnected(NetworkPlayer player){
Network.RemoveRPCs (player);
print ("Player has left. Destroying their objects");
Network.DestroyPlayerObjects (player);
}
However this still throws the error Couldn't send RPC function twice after the player leaves. The RPC that it is failing to send is a RPCMode.All from the host.
Edit: When a player leaves, the host still tries to send a RPC to the disconnected player for about two frames. Is there any way to stop the host's gameobjects from sending the RPC's to the objects that will soon be disconnected
You should be cleaning up the players assets before you remove them.
What is a good way to go about doing that? Tried google searching for a bit about cleaning up assets and had no luck :(
I mean just swap the function calls around. Your removing the player first, then cleaning up their assets, you should do it the other way around.
void OnPlayerDisconnected(NetworkPlayer player){
print ("Player is leaving. Destroying their objects");
Network.DestroyPlayerObjects (player);
Network.RemoveRPCs (player);
}
Does RemoveRPCs, remove rpc's being sent from the player and ones being sent to the player?
Answer by skylem · Jan 13, 2015 at 10:26 PM
im fairly certain ur code just needs to be called in a different order Your trying to remove ur rpcs while ur object exists so it is most likely still sending some before it is destroyed swap ur line
// this way we destroy the player then remove any of the rpcs he // created, Otherwise it will send rpcs after the RPC RemovalProcess // in that tiny window of time between the destroying and removal.
Network.DestroyPlayerObjects (player);
Network.RemoveRPCs (player);
but if that doesnt work try checking that the networkview component still exists before calling the RPC, IE if(networkView != null) {
}
I have tried both of these now and tried switching the order around with no luck.. Is there a way to send a RPC without it wanting a response? Im thinking of a broadcast like a UDP packet would do, compared to a TCP packet. A RPC that waits for no response. So I would just broadcast the last RPC or broadcast a RPC that I'm going to disconnect the player and the network view should be set to null.
The current RPC I am working with that is throwing the error--It's the "idle" animation for my character. So it is being called every frame that i'm not moving basically
Another thought I came up with, is having the "quit" button send an RPC. But I think that would run into the same issue
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Interchange player names in Multiplayer game 1 Answer
Using animator with RPC 1 Answer
How can I send a mouse click to a server using an RPC? 0 Answers
Is server the sender of RPC? 0 Answers