Data Not Sending Over To Client's Photon Playerlist

Options
The image below is shown on my master client.
fOtREpL.jpg
Now, this image below is shown on my newly joined client.
YgvmOHE.jpg

When a new client joins the room, the master will assign him to team two. Master client is always team one.

On the master client, the newly joined player is placed properly in team two. But in the client itself, it appears as team one.

This is my code when a player enters a room (both master and non master):
/// <summary>
    /// Called when a remote player entered the room. 
    /// See the official Photon docs for more details.
    /// </summary>
    public override void OnPlayerEnteredRoom(Photon.Realtime.Player player)
    {
        // Only let the master client handle this connection.
        if (!PhotonNetwork.IsMasterClient)
            return;

        if (player.IsMasterClient == true)
            Debug.Log("Master (nickname: " + PhotonNetwork.NickName + ") has assigned himself a team.");

        // Get the next team index which the player should belong to.
        int teamIndex = GameManager.GetInstance().GetTeamFill();
        // Update the team size the player has been added to of the room.
        PhotonNetwork.CurrentRoom.AddSize(teamIndex, +1);
        // Assign team to the player and update player properties.
        player.SetTeam(teamIndex);

        // Also, player properties are not cleared when disconnecting and connecting automatically.
        // So we have to set all existing properties to null (health, etc).
        // See the PlayerExtensions.cs script for more details.
        // These default values will get overriden by correct data soon.
        player.Clear();

        // The master client sends an instruction to this player for adding him to the game.
        this.photonView.RPC("AddPlayer", player);

        if (player.IsMasterClient == false)
            Debug.Log("A new player has joined the game." +
                      " Master has assigned the new player (nickname: " + player.NickName + ") a team.");
    }


    // Received from the master client, for this player, after successfully joining a game.
    [PunRPC]
    void AddPlayer()
    {
        // Get our selected player prefab index.
        int prefabId = 0;

        // Get the spawn position where our player prefab should be instantiated at, depending on the team assigned.
        // If we cannot get a position, spawn it in the center of the map.
        Transform startPos = GameManager.GetInstance().teams[PhotonNetwork.LocalPlayer.GetTeam()].spawn;
        if (startPos != null) PhotonNetwork.Instantiate(playerPrefabs[prefabId].name, startPos.position, startPos.rotation, 0);
        else PhotonNetwork.Instantiate(playerPrefabs[prefabId].name, Vector3.zero, Quaternion.identity, 0);

        // Get the master client name.
        masterNickname = PhotonNetwork.MasterClient.NickName;
    }

And this is the code that fills the UI texts:
/// <summary>
    /// This method gets called whenever room properties have been changed on the network.
    /// See the official Photon docs for more details.
    /// </summary>
    public override void OnRoomPropertiesUpdate(ExitGames.Client.Photon.Hashtable propertiesThatChanged)
    {
        UpdateUITexts();
    }


    /// <summary>
    /// Update texts displayed on a UI canvas.
    /// </summary>
    void UpdateUITexts()
    {
        // Set the total player count and individual team size texts.
        playerCount.text = "Players: " + PhotonNetwork.CurrentRoom.PlayerCount + "/" + NetworkManagerCustom.GetInstance().maxPlayers;
        int[] size = PhotonNetwork.CurrentRoom.GetSize();
        teamOneCount.text = "Team One: " + size[0];
        teamTwoCount.text = "Team Two: " + size[1];

        foreach (var player in PhotonNetwork.PlayerList)
        {
            int j = -1;
            // Player belongs in team one.
            if (player.GetTeam() == 0)
            {
                // Loop through all team one profiles.
                for (int i = 0; i < teamOneProfiles.Length; i++)
                {
                    // Get the first empty profile.
                    if (teamOneProfiles[i].actorNumber == -1 && j == -1)
                        j = i;

                    // Break the loop here if a profile was already linked to the player.
                    if (player.ActorNumber == teamOneProfiles[i].actorNumber)
                        break;

                    // If the loop reached the end but no player profile was found, add the player to the first empty profile.
                    if (i == teamOneProfiles.Length - 1)
                    {
                        teamOneProfiles[j].actorNumber = player.ActorNumber;
                        teamOneProfiles[j].name.text = player.NickName + "." + player.GetTeam();
                    }
                }
            }
            // Player belongs in team two.
            else if (player.GetTeam() == 1)
            {
                // Loop through all team two profiles.
                for (int i = 0; i < teamTwoProfiles.Length; i++)
                {
                    // Get the first empty profile.
                    if (teamTwoProfiles[i].actorNumber == -1 && j == -1)
                        j = i;

                    // Break the loop here if a profile was already linked to the player.
                    if (player.ActorNumber == teamTwoProfiles[i].actorNumber)
                        break;

                    // If the loop reached the end but no player profile was found, add the player to the first empty profile.
                    if (i == teamTwoProfiles.Length - 1)
                    {
                        teamTwoProfiles[j].actorNumber = player.ActorNumber;
                        teamTwoProfiles[j].name.text = player.NickName + "." + player.GetTeam();
                    }
                }
            }
        }
    }

Comments

  • S_Oliver
    Options
    Im not quite sure. I would like to see "GameManager.GetInstance().GetTeamFill();" the team fill method. But if the team assignment works correctly there should be something with UpdateUITexts();

    But first better check if the team assignment works properly. Since you are using properties use this https://drive.google.com/open?id=1XxtNokTZlkMIlIQ8QMQhZgfKE0Ydeojg to debug . Its a ingame ui, that can be opend with the key - . It shows you all properties^^
  • Vindorable
    edited February 2020
    Options
    Hi @S_Oliver , thank you for taking the time to read my thread and look at my codes!

    Here is the full script of my network manager that handles what to do when a player enters the room: https://pastebin.com/kLeQRd29 (if you'd like to see the overall structure)

    I'm leaving out the UI script because what I've shared above is basically just that. Let me know if you'd like to see the whole script too.

    For the game manager method "GameManager.GetInstance().GetTeamFill();", this is the code below:
        /// <summary>
        /// Returns the next team index a player should be assigned to.
        /// </summary>
        public int GetTeamFill()
        {
            // Initialize variables.
            int[] size = PhotonNetwork.CurrentRoom.GetSize();
            int teamNo = 0;
            int min = size[0];
    
            // Loop over teams to find the lowest fill.
            for (int i = 0; i < teams.Length; i++)
            {
                // If fill is lower than the previous value, store new fill and team for next iteration.
                if (size[i] < min)
                {
                    min = size[i];
                    teamNo = i;
                }
            }
    
            // Return index of lowest team.
            return teamNo;
        }
    
    The full script of the GameManager: https://pastebin.com/KSMjPMT7

    -EDIT01-
    Also, thanks for the debug view package :)
    This is how it looks like on my master client without any new players joining in.
    Two things I noticed in your package is that:
    1. When a new player joins in, the count of players still stays at 1.
    2. Room props can't be read.
    bM3yJPB.jpg
  • S_Oliver
    S_Oliver ✭✭✭
    edited February 2020
    Options
    I updated the DebugView https://drive.google.com/open?id=1Gc4zDBTrdWtX_lPLagyQNDHDsVE0KfuX
    Would you try it again?

    If understand it right. You are using an array for storing Team sizes. And each index is a different Team. That will be stored as Room Property and each Player as a Team property which is just the index ?
  • Thanks man!
    Here are the pictures when the master client has created the room and the new normal client has joined it.

    This is from my master client (note: player count still remains as 1 after the new player joins, not sure why):
    ShFpVUt.jpg

    This is from my normal client:
    THaojuX.jpg

    Yes, team sizes and the number of players in a team are stored in the Room custom property. [team_index (there only 2 team so 0/1), playerCount].

    A player's team (which is an int either 0/1) is stored in Player custom property.
  • S_Oliver
    S_Oliver ✭✭✭
    edited February 2020
    Options
    I tried it myself.
    https://drive.google.com/open?id=1tqWYBz3U6IlA95NMU87zaVXSL90MMYcz
    This is a package that contains all that team assignment stuff, a little bit with abstraction to hide stuff you wont deal often with.

    But from the debug view you can see that the assignment you made is correct, so there has to be little issue in your
    UpdateUITexts
    

    I rly like the idea to use an arry for team stuff.

    EDIT:
    the playerCount updates every 5 secs <- im just using Photon method to retrieve this value
  • Vindorable
    edited February 2020
    Options
    The link you sent is part of your own project? Because it looks different from mine. I imported it into my project with warnings.

    I was thinking that UpdateUITexts might have been the issue all this while but even up till now I'm unable to figure out what is wrong.

    Thanks man :) I still find the debug view package really helpful. I will be using it in my project now onwards.

    -EDIT01-
    I have a Player script. Not sure if this might be causing any issues?
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Photon.Pun;
    using Photon.Realtime;
    
    public class Player : MonoBehaviourPunCallbacks, IPunObservable
    {
        // This method gets called multiple times per second, at least 10 times or more.
        void IPunObservable.OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
        {
    
        }
    
    
        void Start()
        {
    
        }
    
    
        /// <summary>
        /// Helper method for getting the current object owner.
        /// </summary>
        public PhotonView GetView()
        {
            return this.photonView;
        }
    }
    

    -EDIT02-
    I noticed in your debug view code you used 'PhotonNetwork.LocalPlayer.CustomProperties' to get its team index. Using LocalPlayer.

    But in my UpdateUITexts, I used 'foreach (var player in PhotonNetwork.PlayerList)'. Which uses Photon.Realtime.Player. So... am I referencing it wrong or something?

    -EDIT03-
    Ignore -EDIT01-, I don't think the code does anything now so it shouldn't affect in any way.
    I have a PlayerExtension.cs script that has the GetTeam() method. Here's the code: https://pastebin.com/hGT16m4j
    Maybe this might be the issue.
  • S_Oliver
    Options

    No ive just written the code. Because I wanted to try it.

    That's just fine how you referencing the player.

  • I've added -EDIT03- just as I received your new message. Please have a look at that.

    Your code is giving me warnings when I import it into my project.
  • S_Oliver
    S_Oliver ✭✭✭
    edited February 2020
    Options
    I guess you getting erros because of naming clash ?

    But the you dont need this code since yours is already working. You could just take a look at it and see how i have done hits.
  • Been debugging by putting debug logs here and there trying to figure this out for a couple of hours now.. But I can't seem to figure this out. I've removed the for loops and stripped it down to only this:
    void UpdateUITexts()
        {
            // Set the total player count and individual team size texts.
            playerCount.text = "Players: " + PhotonNetwork.CurrentRoom.PlayerCount + "/" + NetworkManagerCustom.GetInstance().maxPlayers;
            int[] size = PhotonNetwork.CurrentRoom.GetSize();
            teamOneCount.text = "Team One: " + size[0];
            teamTwoCount.text = "Team Two: " + size[1];
    
            int p = 1;
            foreach (var player in PhotonNetwork.PlayerList)
            {
                int j = -1;
                Debug.Log("Players in room: " + PhotonNetwork.PlayerList.Length);
                Debug.Log(p + ": " + player.NickName + " (team: " + player.GetTeam() + ")" + j);
                p++;
            }
        }
    
    Still shows wrong...
    PKdovCw.jpg
    I'm starting to feel PhotonNetwork.LocalPlayer is not the same as Photon.Realtime.Player of the same client.. I'm stuck.
  • S_Oliver
    Options
    PhotonNetwork.LocalPlayer returns a Photon.Realtime.Player object of the local player :p
    Hit me on discord Daegit#4499.
    So we can talk quicker. And you might want to share you project so i can take alook at it directly.

  • S_Oliver
    S_Oliver ✭✭✭
    Options
    Solved
  • roundy
    Options

    What was the problem? Have the same situation.

    PhotonNetwork.CountOfPlayers not returning correct value for Master Client, only joined clients.

    Need to show player count on region.

    Thank you