Master client background issue with photon (PUN) in unity

Options
I'm making a multiplayer game like tambola (housie) using photon (PUN) in unity3d where random number is generating in the master client. That generated number is send to all clients using RPC. Now my problem is when my master client goes in the background, master client doesn't able to send RPC to other clients. Because of that game freezes in all the connected players.

So any workaround for this? Where game won't freeze and random numbers keep on generating though my master client goes in the background.

TIA

Comments

  • OneManArmy
    Options
    You can use room custom properties for such things.
  • Apurva
    Options
    But my gameplay is managed from master client, when masterclient go in background, it can not generate random number and that's why RPC is not calling from master client and new numbers can't display in all the other clients.
  • OneManArmy
    Options
    mobile game? master generates this random number constantly, or just once?
  • Apurva
    Options
    Yes mobile game.
    It's generating constantly after 10 seconds of gap. Random numbers generation code is written in the coroutine. and that code is executing only on the master client. When it generates new number, it send RPC. Total 90 numbers are generating on 10 seconds of interval.
  • OneManArmy
    Options
    Maybe you can switch master OnApplicationFocus/OnApplicationPause?
  • JohnTube
    JohnTube ✭✭✭✭✭
    Options
    Hi @Apurva,

    Thank you for choosing Photon!

    You can try switching Master Client as soon as the current one moves to the background or is no longer available, something like this:
    using Photon.Pun;
    using UnityEngine;
    
    public class SwitchMasterClientAsap : MonoBehaviour
    {
    
        private void OnApplicationQuit() 
        {
            this.SwitchOrDisconnect();
        }
    
        private void OnApplicationPause(bool paused)
        {
            if (paused)
            {
                this.SwitchOrDisconnect();
            }
        }
    
        private void OnApplicationFocus(bool focused)
        {
            if (!focused)
            {
                this.SwitchOrDisconnect();
            }
        }
    
        private void SwitchOrDisconnect()
        {
            if (PhotonNetwork.IsMasterClient)
            {
                // todo: also check Application.runInBackground and platform e.g. iOS does not allow background thread 
                if (PhotonNetwork.PlayerList.Length > 1 && PhotonNetwork.KeepAliveInBackground > 0f)
                {
                    PhotonNetwork.SetMasterClient(PhotonNetwork.LocalPlayer.GetNext());
                }
                else
                {
                    PhotonNetwork.Disconnect();
                }
                PhotonNetwork.SendAllOutgoingCommands(); // send it right now
            }
        }
    }
  • Apurva
    Options
    Thanks for your explanatory answer. @JohnTube @OneManArmy