MasterClient thinks that they are everyone

Okay, so the tittle is a bit weird but let me explain: I wanted to label which player's are the masterClient and which are not. I did this by doing the following:

Step 1. I created a function that gets called when a player joins a room. This function transmits the basic stats over to the other players.

[code2=csharp]public void OnPhotonPlayerConnected (PhotonPlayer id)
{
Debug.Log("Player connected");
foreach(Player pl in PlayerList)
{
transform.GetComponent<PhotonView>().RPC("Client_PlayerJoined", id, pl.PlayerName, pl.OnlinePlayer);
}
}[/code2]

Step 2. I compared the id of the player that has just connected to the id of the MasterClient. Temp here is just a placeholder for the "Player" class (which holds all the information for each individual player in the room - score, name, ping, etc.)

[code2=csharp][RPC]
public void Client_PlayerJoined(string Username, PhotonPlayer id)
{
bool NewTemp = true;
Player temp = new Player();
temp.PlayerName = Username;
temp.originalName = PlayerName;
temp.OnlinePlayer = id;
if(PhotonNetwork.masterClient == id)
{
Debug.Log("This player is the MasterClient!");
temp.isMasterClient = true;
}
else
{
temp.isMasterClient = false;
}
//The code goes on, but it doesn't really relate to the topic anymore (no more MasterClient stuff)[/code2]

This works fine for players that are NOT the MasterClient (they find that the id belongs to the MasterClient and vice versa), but the actual MasterClient always thinks that current player has the same id as them (therefore sets "isMasterClient" true for that player). Would anyone know why? I even added a little security at the end of the code - without results.

[code2=csharp]if(PhotonNetwork.isMasterClient == true)
{
if(temp.isMasterClient == true)
{
if(temp.OnlinePlayer != PhotonNetwork.player)
{
temp.isMasterClient = false;
}
}
}[/code2]

Comments

  • Each player probably has a prefab associated with it?
    Can you just add a small script to that prefab that checks if its photon view is the master client when it is instantiated?
    Then it would run on each players game once s/he joins and is instantiated?
    (still learning myself so could be completely wrong)
  • Thanks for your reply, but that would not work unfortunately :/
  • Can you explain why it would not work?
    PhotonNetwork.isMasterClient refers to "this" client. Not the the new players.
    You can't set isMasterClient. It is a value that's derived from the player IDs of active players in the room.
  • Well if I knew why it would not work, it would make things a lot easier :D If I did a check on when the player object is instantiated, then the check would only be local (not networked). If it works for perfectly for people that are not the MasterClient, then I don't see why it shouldn't work for everyone else. I actually noticed that I actually run "Client_PlayerJoined" over the MasterClient. I thought this might have something to do with it:

    [code2=csharp]public void OnJoinedRoom()
    {
    transform.GetComponent<PhotonView>().RPC("Server_PlayerJoined", PhotonTargets.MasterClient, PlayerName, PhotonNetwork.player);
    }


    [RPC]
    public void Server_PlayerJoined(string Username, PhotonPlayer id)
    {
    transform.GetComponent<PhotonView>().RPC("Client_PlayerJoined", PhotonTargets.All, Username, id);
    }[/code2]
  • I'm lost.
    I gave you an answer and you replied "that would not work". My question to that was: Why do you say that? Did you try it and it DID not work or do you just say it WOULD not work? And: If you tried it and it DID not work: How does it fail?

    You are basically asking others to debug your code. That's fine and all but you have to realize we don't have that code and we don't know what fails how.
    And again: You don't need an RPC telling others that some player joined. That is covered by PUN. If there is a reason why you can't use the callbacks and info from PUN, let us know and maybe we can add the missing info or give hints how to add it.
  • Actually I just wanted to know if anyone knew how the id of the newly joined player can equal to that of the MasterClient.... :D of course its always easy to go in afterwards and do a check to see if the player is actually the MasterClient, then change all the other players in the list so that "isMasterClient" is set to false (which is what I am doing as a placeholding solution). What I just want to understand why it wouldn't work the way I have it here (you have all the code that involves this issue).

    The reason that I use classes for each player and have my own Player list is mainly due to the fact that I like to manage things that way and find it pretty easy to debug and update player information.
  • By design, the ID of a newly joined player can't match the one of the Master Client.
    In the code you posted, you didn't compare the IDs, however:
    if(PhotonNetwork.masterClient == id)

    The variable masterClient is of type PhotonPlayer. It's not an int. The code you post can't even compile, so I wonder how I should have all relevant code.
    I don't know how I should help here without a complete re-write.
  • The variable "id" is of type PhotonPlayer....
    [code2=csharp]Client_PlayerJoined(string Username, PhotonPlayer id)[/code2]
  • Yes, but PhotonNetwork.masterClient should not be of type int, if I recall correctly.
    Maybe that changed but if you are using a older version: Please update to PUN 1.25 and we can check if the issue persists and fix it there.
  • I think I found the fix! The problem occured to me when trying to debug another problem. It has to do with the fact that I call the function over the master client himself. This causes confusion sometimes, or at least in this case, because I tried to call another RPC while calling an RPC over the masterclient (its all kinda confusing)....
  • Hehe. Yes, it sounds kinda confusing. :)
    I'll just read it as "solved" for the moment. Hope that's ok?
  • Yes sure :)