OnMasterClientSwitched









How do I use On Master Client Switched
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: WorkerInGame.cs Copy
34     public void OnMasterClientSwitched(PhotonPlayer player)
35     {
36         Debug.Log("OnMasterClientSwitched: " + player);
37
38         string message;
39         InRoomChat chatComponent = GetComponent(); // if we find a InRoomChat component, we print out a short message
40
41         if (chatComponent != null)
42         {
43             // to check if this client is the new master...
44             if (player.isLocal)
45             {
46                 message = "You are Master Client now.";
47             }
48             else
49             {
50                 message = player.name + " is Master Client now.";
51             }
52
53
54             chatComponent.AddLine(message); // the Chat method is a RPC. as we don't want to send an RPC and neither create a PhotonMessageInfo, lets call AddLine()
55         }
56     }
File name: GameLogic.cs Copy
67     public void OnMasterClientSwitched()
68     {
69         Debug.Log("OnMasterClientSwitched");
70     }
File name: NetworkingPeer.cs Copy
690     private void CheckMasterClient(int leavingPlayerId)
691     {
692         bool currentMasterIsLeaving = this.mMasterClient != null && this.mMasterClient.ID == leavingPlayerId;
693         bool someoneIsLeaving = leavingPlayerId > 0;
694
695         // return early if SOME player (leavingId > 0) is leaving AND it's NOT the current master
696         if (someoneIsLeaving && !currentMasterIsLeaving)
697         {
698             return;
699         }
700
701         // picking the player with lowest ID (longest in game).
702         if (this.mActors.Count <= 1)
703         {
704             this.mMasterClient = this.mLocalActor;
705         }
706         else
707         {
708             // keys in mActors are their actorNumbers
709             int lowestActorNumber = Int32.MaxValue;
710             foreach (int key in this.mActors.Keys)
711             {
712                 if (key < lowestActorNumber && key != leavingPlayerId)
713                 {
714                     lowestActorNumber = key;
715                 }
716             }
717
718             this.mMasterClient = this.mActors[lowestActorNumber];
719         }
720
721         // make a callback ONLY when a player/Master left
722         if (someoneIsLeaving)
723         {
724             SendMonoMessage(PhotonNetworkingMessage.OnMasterClientSwitched, this.mMasterClient);
725         }
726     }
File name: NetworkingPeer.cs Copy
757     internal protected bool SetMasterClient(int playerId, bool sync)
758     {
759         bool masterReplaced = this.mMasterClient != null && this.mMasterClient.ID != playerId;
760         if (!masterReplaced || !this.mActors.ContainsKey(playerId))
761         {
762             return false;
763         }
764
765         if (sync)
766         {
767             bool sent = this.OpRaiseEvent(PunEvent.AssignMaster, new Hashtable() { { (byte)1, playerId } }, true, null);
768             if (!sent)
769             {
770                 return false;
771             }
772         }
773
774         this.hasSwitchedMC = true;
775         this.mMasterClient = this.mActors[playerId];
776         SendMonoMessage(PhotonNetworkingMessage.OnMasterClientSwitched, this.mMasterClient); // we only callback when an actual change is done
777         return true;
778     }
File name: PhotonClasses.cs Copy
437         public virtual void OnMasterClientSwitched(PhotonPlayer newMasterClient)
438         {
439         }
File name: InRoomRoundTimer.cs Copy
76     public void OnMasterClientSwitched(PhotonPlayer newMasterClient)
77     {
78         if (!PhotonNetwork.room.customProperties.ContainsKey(StartTimeKey))
79         {
80             Debug.Log("The new master starts a new round, cause we didn't start yet.");
81             this.StartRoundNow();
82         }
83     }

OnMasterClientSwitched 304 lượt xem

Gõ tìm kiếm nhanh...