Photon team assignment
So I was trying to create a photon script that assigns users to a team and then displays them on separate panels once the match is starting. I will show you the code and the results first. The code:
Assign me to a team after I join the room:
void OnJoinedRoom(){
Debug.Log(PhotonNetwork.playerList.Length);
Debug.Log(PhotonNetwork.room.MaxPlayers);
if(PhotonNetwork.playerList.Length <= PhotonNetwork.room.MaxPlayers/2){
Debug.Log("Assigned Red");
PhotonNetwork.player.SetCustomProperties(new Hashtable() {{"team","red"}});
}else{
Debug.Log("Assigned Blue");
PhotonNetwork.player.SetCustomProperties(new Hashtable() {{"team","blue"}});
}
joined = true;
}
When 4 players joined, start the game ( this lies in void Update() ) :
if(joined && PhotonNetwork.playerList.Length == 4 && !doOnce){
Debug.Log("Start");
doOnce=true;
//this.GetComponent<PhotonView>().RPC ("StartGame", PhotonTargets.All);
StartGame();
}
StartGame():
void StartGame(){
inRoomPanelGo.SetActive(false);
champSelectGo.SetActive(true);
started = true;
}
Once started is set to true, and players go into champ select this code runs. Also in void Update() :
if(started){
foreach(PhotonPlayer pl in PhotonNetwork.playerList){
Debug.Log(pl.NickName + " IS HERE " + "AND HE IS TEAM " + pl.customProperties["team"]);
if(pl.customProperties["team"]=="blue"){
Debug.Log(pl.NickName + " " + pl.customProperties["team"]);
GameObject spawnedCsPlayerBl = Instantiate(csPlayerPrefabBlue, Vector3.zero, Quaternion.identity)
as GameObject;
spawnedCsPlayerBl.transform.SetParent(blueContainer, false);
spawnedCsPlayerBl.transform.GetChild(0).GetComponent<Text>().text=pl.NickName;
}else{
Debug.Log(pl.NickName + " " + pl.customProperties["team"]);
GameObject spawnedCsPlayerRd = Instantiate(csPlayerPrefabRed, Vector3.zero, Quaternion.identity)
as GameObject;
spawnedCsPlayerRd.transform.SetParent(redContainer, false);
spawnedCsPlayerRd.transform.GetChild(0).GetComponent<Text>().text=pl.NickName;
}
}
started=false;
}
The last piece of code basically checks for all the players in the room and based on their "team" tag it instantiates a Image UI ( with a text that reads their name ) in the correct spot ( blue panel or red panel ). But the results are totally random. The reading is actually successful. Here is the from the debugging in the last function:
IMPORTANT: The order that you see in the debug log is the order i joined the room.
TestUser1 -> TestUser2 -> TestUser3 -> Sphades
The debug log ran on the "Sphade" instance which was ran in the Editor. The other 3 where ran from a built Standalone.
The debug looks fine. The results however...
Note: Left is blue side Right is red side
TestUser1:
TestUser2:
TestUser3:
Sphades:
As you can see, the order in the actual UI is totally random. But they get assigned well in the Debug. And I can't figure out why.
One more thing:
If I run this part of code:
if(started){
foreach(PhotonPlayer pl in PhotonNetwork.playerList){
Debug.Log(pl.NickName + " IS HERE " + "AND HE IS TEAM " + pl.customProperties["team"]);
if(pl.customProperties["team"]=="blue"){
Debug.Log(pl.NickName + " " + pl.customProperties["team"]);
GameObject spawnedCsPlayerBl = Instantiate(csPlayerPrefabBlue, Vector3.zero, Quaternion.identity)
as GameObject;
spawnedCsPlayerBl.transform.SetParent(blueContainer, false);
spawnedCsPlayerBl.transform.GetChild(0).GetComponent<Text>().text=pl.NickName;
}else if(pl.customProperties["team"]=="red"){
Debug.Log(pl.NickName + " " + pl.customProperties["team"]);
GameObject spawnedCsPlayerRd = Instantiate(csPlayerPrefabRed, Vector3.zero, Quaternion.identity)
as GameObject;
spawnedCsPlayerRd.transform.SetParent(redContainer, false);
spawnedCsPlayerRd.transform.GetChild(0).GetComponent<Text>().text=pl.NickName;
}
}
started=false;
}
Instead of:
if(started){
foreach(PhotonPlayer pl in PhotonNetwork.playerList){
Debug.Log(pl.NickName + " IS HERE " + "AND HE IS TEAM " + pl.customProperties["team"]);
if(pl.customProperties["team"]=="blue"){
Debug.Log(pl.NickName + " " + pl.customProperties["team"]);
GameObject spawnedCsPlayerBl = Instantiate(csPlayerPrefabBlue, Vector3.zero, Quaternion.identity)
as GameObject;
spawnedCsPlayerBl.transform.SetParent(blueContainer, false);
spawnedCsPlayerBl.transform.GetChild(0).GetComponent<Text>().text=pl.NickName;
}else{
Debug.Log(pl.NickName + " " + pl.customProperties["team"]);
GameObject spawnedCsPlayerRd = Instantiate(csPlayerPrefabRed, Vector3.zero, Quaternion.identity)
as GameObject;
spawnedCsPlayerRd.transform.SetParent(redContainer, false);
spawnedCsPlayerRd.transform.GetChild(0).GetComponent<Text>().text=pl.NickName;
}
}
started=false;
}
Then the red part is ignored completly. Als if pl.customProperties["team"] is not equal to "red". Which obviously is as you can see in the debug log.
I can't wrap my head around it. If needed more details, ask. Please help me on this one!
Answer by ChristianSimon · Jun 19, 2017 at 08:01 AM
Hi,
it seems that the Player Properties are not updated on every client, when you try to access them. You have the following condition if(pl.customProperties["team"]=="blue") {...} else {...}
. If Player Properties are not updated when trying to access them here, the 'else' branch will always be processed and furthermore players are always marked as team 'red' in this case.
You should also try to avoid calling StartGame();
from the Update() function. Another option you have here is to use OnPhotonPlayerConnected(PhotonPlayer newPlayer)
callback on the MasterClient. The MasterClient can check the player count whenever this callback is executed and inform the other players to start the game, e.g. by using a RPC or RaiseEvent function. You might want to add some short delay before updating the UI as well to make sure that each client has received the up-to-date Player Properties.
You can also consider about taking a look at PunTeams script which comes with the PUN package from the Asset Store. It basically does the same as you have implemented and also uses the Player Properties for storing the current team.
Your answer
Follow this Question
Related Questions
Photon camera problum 0 Answers
Unity Photon doesn't Spawn Player 0 Answers
Lerping to Smooth Network Movement 0 Answers
PHOTON FindFriends (222) not allowed on current server (GameServer) 0 Answers