Search Unity

Clean way to leave a game

Discussion in 'Multiplayer' started by SpeakUpGames, Oct 17, 2015.

  1. SpeakUpGames

    SpeakUpGames

    Joined:
    Nov 25, 2014
    Posts:
    56
    Is there a proper, one-function way to leave a game while it's still going on? I'm having difficulty finding it in the docs.
    I'm guessing it might be something like StopClient() or StopHost(), but then you need to differentiate between them and it seems messy. There must be a simple function to leave a game regardless of client/host and act appropriately for it.
     
  2. l3fty

    l3fty

    Joined:
    Mar 23, 2013
    Posts:
    87
    Ah yes, I'm being lazy at the moment and calling all of them (StopClient, StopServer & StopHost) when I quit so I don't have to differentiate. It doesn't cause any problems that way, but I suppose we could just pass through a new method which checks the status of isServer and isClient and only calls the appropriate stop methods.

    Code (CSharp):
    1. public void QuitGame () {
    2.  
    3.     if (isClient && isServer)
    4.        StopHost();
    5.     else if (isClient)
    6.        StopClient();
    7.     else
    8.        StopServer();
    9. }