Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to deal with Host player turning off screen of their mobile device

Discussion in 'Multiplayer' started by Kurius, Dec 27, 2018.

  1. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    I noticed that once the Host player turns off the screen of their mobile device, then Client players can no longer call any [Command] methods. What's the best way to deal with this? Somehow have the Host player send a message to all Client players that the game can't resume until the Host player turns his screen back on? Thanks
     
  2. Tiny-Tree

    Tiny-Tree

    Joined:
    Dec 26, 2012
    Posts:
    1,314
    use on application pause event to change host
     
    Joe-Censored and Kurius like this.
  3. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    It looks like changing which player is the Host is complicated, as per... https://docs.unity3d.com/2017.4/Documentation/Manual/UNetHostMigration.html

    So instead I just do Game Over if the Host turns off their screen, via this workaround...
    Whenever a client player performs an action that involves a call to a "Cmd" method, I first call myCheckConnection().

    Code (CSharp):
    1.     void myCheckConnection(){
    2.         if (!checkingConnection) {
    3.             checkingConnection = true;
    4.             PlayerPrefs.SetInt ("hostLeft", 1);
    5.             PlayerPrefs.Save ();
    6.             StartCoroutine (myVerifyConnection ());
    7.             CmdSendConnectionCheckToServer ();
    8.         }
    9.     }
    10.  
    11.     [Command]
    12.     void CmdSendConnectionCheckToServer()
    13.     {
    14.         RpcSendConnectionCheckToClients();
    15.     }
    16.  
    17.     [ClientRpc]
    18.     void RpcSendConnectionCheckToClients()
    19.     {
    20.         PlayerPrefs.SetInt ("hostLeft", 0);
    21.         PlayerPrefs.Save ();
    22.     }
    23.  
    24.     IEnumerator myVerifyConnection(){
    25.         yield return new WaitForSeconds (1f);
    26.         if(PlayerPrefs.GetInt("hostLeft")==1){
    27.             SceneManager.LoadScene ("GameOver");
    28.         }
    29.         checkingConnection = false;
    30.     }
    31.