The Photon Forum
is Closed Permanently.

After many dedicated years of service, we have made the decision to retire our Forum and switch to read-only: we´ve saved the best to last! Your search result can be found below. Plus, we offer support via these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

How to Switch Master Client?

Pav
2016-12-28 21:17:23

Good day,

My biggest issue before putting out my android game is now related to the master client putting the app into the background, or disconnecting.

The game looks "paused" or the other players get disconnected automatically.

I don't really understand how to switch the masterclient, along with the objects that it instantiated.

How would I tackle this problem?

Comments

JannickL
2016-12-28 21:37:36

@Pav If a client leaves a room all his instantiated objects will get destroyed.
The MasterClient switches automatically when he leaves.
Use photon network instantiate scene object instead of the normal PhotonNetwork.Instantiate.

Notice that only the masterclient can change the transform of those scene objects but they will not get destroyed when the mc leaves or a another player becomes the mc.

Greets

Pav
2016-12-28 22:10:26

Ah, I see.
I did a lot of searching before I put up this question, wasn't aware that the master client switches automatically if that player leaves.

What about cases regarding having the app in the background?
Or it'll be fine as long as I use networkinstantiate scenebject

JannickL
2016-12-28 22:13:28

@Pav Check out if the player stills sends data when the app is backgrounded. If the player stays connected and sends data you will not have a problem. Btw unity got a allow background settings but i don't know if it works with mobile too.

Pav
2016-12-28 22:36:41

I tried "PhotonNetwork Instantiate Scene Object" but because it has a photonview for transform, it's frozen when the masterclient is running the app in background.

JannickL
2016-12-28 22:55:44

@Pav there you go: http://answers.unity3d.com/questions/11085/unity-freezes-when-window-inactive-any-workaround.html

Pav
2016-12-29 02:54:23

I see. That doesn't work with android unfortunately, but I figured out how to determine if the masterclient is paused. https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationPause.html

However, I haven't played around with getting photonPlayer ID's and I'm stuck at..

Pav
2016-12-29 04:11:26

Alright. I got it to switch masterclients, but it doesn't work properly.

When current masterclient puts the app into background, it doesn't transfer. Only transfers when the app comes back into focus.

Pav
2016-12-29 21:01:45

@JannickL Ok, it works properly now, How could I choose the the next playerID to be the master client? so far I just do..

PhotonNetwork.SetMasterClient(PhotonNetwork.playerList[1]);

JohnTube
2016-12-29 22:29:18

@Pav @JannickL

ICYMI: there is a documentation page about "Master Client and Host Migration".

Pav
2016-12-30 03:22:03

@JohnTube I've seen that, and it makes sense. I've made a script that switches the masterclient, however, it's based on OnPhotonSerializeView.

Would you suggest doing PunRPC instead?

JohnTube
2016-12-30 10:45:02

@Pav
I'm not a PUN expert. I'm used to plain Photon events (RaiseEvent).
My other colleagues are on vacation so I can't ask them now.
But IMO, PunRPC is better than OnPhotonSerializeView.

In any case I think you should switch the MasterClient from another client that detects the MasterClient disconnection and use this:

  
PhotonNetwork.SetMasterClient(PhotonNetwork.masterClient.GetNext());

Pav
2016-12-30 17:14:45

@JohnTube That works even better than what I had. Appreciate it :)

wtdunity
2018-02-05 11:56:23

*******Best Working way to change Master Client in photon network.******
void OnApplicationPause(bool pauseStatus)  
    {     

        if (pauseStatus)  
        {  
            if (PhotonNetwork.connectionState == ConnectionState.Connected && PhotonNetwork.inRoom && PhotonNetwork.isMasterClient)  
            {   
                // Game is in Background | pause | quit  
                
                //PhotonNetwork.BackgroundTimeout = 2f;                                                  
                ChangeMasterClientifAvailble();  
                PhotonNetwork.SendOutgoingCommands();  
                 
            }  
        }  
        else  
        {  
             
            if (PhotonNetwork.connectionState == ConnectionState.Connected && PhotonNetwork.inRoom && PhotonNetwork.isMasterClient)  
            {                  
               // do whatever you want . if you not reach time to live player then you are connected and inroom already.  
            }

            //check if game end ||room destroyed ||timeout  
        }          
    } // End OnApplication Pause.

     
   ///   
   /// Changes the master client if availble.  
   ///   
    public void ChangeMasterClientifAvailble()  
    {  
        if (!PhotonNetwork.isMasterClient)  
        {  
            return;  
        }  
        if (PhotonNetwork.room.PlayerCount <= 1)  
        {  
            return;  
        }         
          
        PhotonNetwork.SetMasterClient(PhotonNetwork.masterClient.GetNext());                   
    }


 public virtual void OnMasterClientSwitched(PhotonPlayer newMasterClient)  
    {  
 //event will fired to all connected players in same room.  
//if master client goes in background then new master client info hear.   
}

illogical
2020-01-12 11:38:03

Hi, i am using this method. could it be any better? or am i doing it wrong?

Djebedia
2020-01-12 12:21:23

As I understand the masterclient is automatically switched by photon on current mc got disconnected. You just have to setup plugin properly.
And the problem was with an app going to background, and in that case you should use OnApplicationPause/Focus to check if you are mc and tell others you are about to go to background via rpc.
I'm not an expert, so its just things to think about )

illogical
2020-01-12 12:49:37

Hi @Djebedia thanks for your response. However the OP is 4 years old. I am sorry i had to resurrect it.

what do you think of my code above? is it good idea to check latency using RPC? I am following the idea of "heartbeat" in this photon documentation here

I dont think of any other method but correct me if im wrong

JohnTube
2020-01-13 10:42:12

I have answered here.

juniot52999
2021-08-19 16:46:12

I'm looking if there's a way to set another player to Master Client during game.

Back to top