0
\$\begingroup\$

I am developing a two player multiplayer game. Since its only two players, I don't want to keep my connection open if a player disconnects.

Now this works well from the host side, through usage of StopHost(). If the player who hosts disconnects, it disconnects both the players from the game.

But using StopHost() in the client(Player B) side does not remove the host player(Player A). In other words, the client is out of the game, but the host is still in the game.

So, what I want is if one player(host or the client whoever it is) disconnects, I want both of them to be disconnected.

So, how can I do it? Please help me with this. I have almost lost hope with UNET. Thanks

EDIT:

My setup is like this, my LobbyManager comprises of UI elements like "TitlePanel", "MainPanel", etc. The Disconnect button is part of the title panel and gets enabled on press of Esc key. The button is linked to a script attached to the "TitlePanel". This script uses a MonoBehaviour which I changed to NetworkBehaviour in order to use [Command]. But it doesn't work as my "Title Panel" does not have a NetworkIdentity. Adding one creates even more problems

So, I thought if there was a way to send mesages from client to server without NetworkBehaviour, then maybe I could send a flag there to the server.

So is it possible?

\$\endgroup\$
  • \$\begingroup\$ Don't force the game to stop via code, just deal with requests to make sure anyone isn't allowed to play if there aren't at least two players \$\endgroup\$ – liggiorgio Oct 25 '16 at 14:23
  • \$\begingroup\$ Now that's the problem. Both the host and the client are already in the game. And when the client leaves, host doesn't. For now, I either want the host to be sent back to the lobby scene and start waiting for a new player or atleast be disconnected and start over again. How to do that? \$\endgroup\$ – Gabriel Oct 26 '16 at 4:55
  • \$\begingroup\$ Also, I was wondering if and when a client disconnects, does it invoke any callback methods on the host? If so, I could try editing that method. \$\endgroup\$ – Gabriel Oct 26 '16 at 4:57
  • \$\begingroup\$ Ok. I think I know where I have gone wrong. I have edited my question. Please check \$\endgroup\$ – Gabriel Oct 26 '16 at 7:56
0
\$\begingroup\$

On your custom NetworkManager you can override the OnServerDisconnect for that purpose. Your code should looks like this:

public override void OnServerDisconnect(NetworkConnection conn)
{
    NetworkServer.DestroyPlayersForConnection(conn);
    StopHost();
}

OnServerDisconnect means: on the server side a client has disconnected. I know, the method name does not suggest that :/

I also know that this is a question from 2016 but as it may still hunt people at night, I'll left this answer here. Cheers ;)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.