Room Switching with MasterClient

Options
Hi, I am making a 3v3 Brawl. The way it goes is that 3 people enter room A (staging room) and ready up. Another 3 enter room B (staging room). Once each room ready up, the Masterclient leaves the room and searches for Room C (Battle Room) or Creates it. I am using the friend approach so that the 2 other players follow the masterclient(slot reservation).

Problem is that since I am using the OnMasterClientSwitched() for if a masterclient leaves a room, well guess what....the new masterclient isn't able to follow the old masterclient which is now in Room C. Can't see to figure it out.....

Here is my code :
public IEnumerator Test()
    {
        if(!PhotonNetwork.IsMasterClient)
        {
            StartCoroutine(FindJoinMasterRoom());
        }
        else if (PhotonNetwork.IsMasterClient)
        {
            List<string> playerInRoom = new List<string>();
            foreach (var player in PhotonNetwork.PlayerListOthers)
            {
                playerInRoom.Add(player.UserId);
            }

            PhotonNetwork.LeaveRoom();
            yield return new WaitForSeconds(1);

            if (PhotonNetwork.IsConnected)
            {
                string roomID = Random.Range(0, 10000) + Random.Range(0, 10000).ToString();
                RoomOptions room = new RoomOptions { MaxPlayers = 2, IsOpen = true, IsVisible = true };
                PhotonNetwork.JoinOrCreateRoom(roomID, room, TypedLobby.Default, playerInRoom.ToArray());
                //Create BattleRoom properties
            }
        }
    }

    private IEnumerator FindJoinMasterRoom()
    {
        //Get the master UserID
        var masterUserID = PhotonNetwork.MasterClient.UserId;

        if (masterUserID != null)
        {
            PhotonNetwork.LeaveRoom();
            yield return new WaitForSeconds(1);

            if (PhotonNetwork.IsConnected)
            {
                
                PhotonNetwork.FindFriends(new string[1] { masterUserID });
                yield return new WaitForSeconds(1);

                FriendInfo roomMaster = PhotonFriends.Find(x => x.UserId == masterUserID);
                PhotonFriends.Remove(roomMaster);

                if (roomMaster.IsInRoom)
                {
                    PhotonNetwork.JoinRoom(roomMaster.Room);
                }
            }
        }
    }

If there is a better way to go about it, I would really appreciate it. Thanks

Answers

  • Tobias
    Options
    The way you approach this is OK, if you really need to form groups before you actually play. When staging is done, let the Master Client send a message "follow me" (e.g. RaiseEvent with EventCode 1) to All / AllViaServer. Then you disable the code that reacts to MasterClientSwitched (as everyone should follow the lead).
    So in essence you need a state to react properly to the Master Client switch...

    Alternatively, it might be better to arrange sides/teams in the room and just use it to play, too. In many cases, it might be as good to just arrange teams (force them to be equal) start the match. Set IsVisible and IsOpen to false and go.
  • dibset
    dibset
    edited August 2021
    Options
    Alternatively, it might be better to arrange sides/teams in the room and just use it to play, too. In many cases, it might be as good to just arrange teams (force them to be equal) start the match. Set IsVisible and IsOpen to false and go.

    I have a few questions about this :

    1. Wouldn't there be only 1 masterclient in this case? Would it cause a problem down the line?
    2. Would the client still be able to see only his TEAM A on screen and not TEAM B and vice versa ? OR the client would see all 6 players on screen ? Or is it my game logic that determines that ?

    I'm trying to mimic MOBA games

    Thanks
  • Tobias
    Options
    To play a game with 2 teams, everyone needs to be in one room. So you end up in one room anyways.

    The organization into 2 teams can be done by one Master Client, no problem.

    How much any player notices about it's own team or the other, is up to your UI and some organization of updates:
    You can use Interest Groups to make up a channel per team, which the other team doesn't listen to. This can be hacked, yes, but it's a simple way to get your game done and worrying about hacking too early and too much can mean you never finish.
  • dibset
    Options
    Thanks for the reply and advise @Tobias. I will look into Interest Groups :)