I have an online game where two parties can join for a match. There’s a “Disconnect” button that when pressed sends an RPC to all players to disconnect from the server. See code below:
[RPC]
public void Disconnect()
{
Network.Disconnect();
}
// DISCONNECT BUTTON
if (isConnected)
{
if (GUI.Button(new Rect(Screen.width/2, 500, 200, 200), "Disconnect", style))
{
// disconnect everyone
nV.RPC("Disconnect", RPCMode.AllBuffered);
isConnected = false;
isInitialized = false;
// reload scene
Application.LoadLevel(1);
}
}
The thing is, after the scene is reloaded, I need Network.isServer to be false again but it stays with “true” value. Shouldn’t Network.isServer go false on the server once it’s disconnected? Is there any way to trigger this boolean value change? I know it’s a read value only but there’s got to be a way to make that change on its own.
RPCs had a little problem with the RPCMode “All” and “AllBuffered”. As fas as i remember it works well, except for the case when executed on the server. Everyone will receive the RPC call except the server itself. The solution usually was to execute the method manually on the server. Since that problem only exists when the RPC is invoked on the server that’s usually not a big problem. So instead of “AllBuffered” you might simply use “Others” and right after the RPC call you simply add: Disconnect();
Though the RPC is actually quite pointless since when you simply call Disconnect on the server all clients will be dropped automatically as the server is closing.
edit
I just read that you want to reload the scene. Do you try to immediately restart a new server? That won’t work. The networking happens on a seperate thread. [Network.Disconnect][4] doesn’t take immediate effect. However if you want to restart the server, why do you want to shut it down completely?
Anyways if you want to shut it down completely you might want to use a coroutine like this instead:
That will call Network.Disconnect(); and wait until the peerType has changed to “Disconnected”, in which case isServer should also be back to “false”. Btw afaik isServer and isClient just return the peerType state ^^.