Show user level with Photon

Options
Well, how wonderful. Im using Photon. Okay i have a playerprefs for the playerlevel. I have also a mask and a grid were you can see all joined players included there level. So when a player joined the room. A UI obj is instantiated within 2 objects as child. (Playername and playerlevel)
Playername is already working but the playerlevel is a great mess since its info comes from a playerprefs. How can I sync the playerlevel for all joined people?

public Text Playerlevel;

void Start ()
{
Playerlevel.text = "level " + Playerprefs.GetFloat ("Playerlvl");
}

void Update ()
{
if (photonView.isMine)
....
}

It drives me crazy. Playerlevel.text is the child of the "roommask"
Please help. This is a very great problem.

Best Answer

Answers

  • Hi @IBuckly,

    the PlayerPrefs are not synchronized across by default, means that no other player has access to them. If you want to 'share' a value from the PlayerPrefs you can use the player's custom properties. When having joined a room you can use PhotonNetwork.player.SetCustomProperties(Hashtable ht); in order to add a value from the local player's PlayerPrefs. These custom properties are shared across the network so any player can access them. When they are updated there is callback you can use adjust the UI. This is OnPhotonPlayerPropertiesChanged(object[] playerAndUpdatedProps). You can find some more information and how to use it over here.
  • IBuckly
    Options
    Okay so i am not familiar with the custom player properties. What do you suggest me to do? How can I make custom player properties and how can I use it when needed. I am looking for some documentation. But nothing found. Maybe you can help me?
  • IBuckly
    Options
    I will try this as soon as possible. So I need to put the first part in de object that need to set the custom player properties. And the second part in the other object that needs to call it. Thanks for now. If it doesn't work well, you will hear it from me.

    Btw good work, i have seen your videos on youtube. Really great work!

    You will hear soon from me
  • IBuckly
    Options
    Okay, so I have tried it, but no idea what to do exactly. I have and empty gameobject called the networkmanager. If a player joined a room, there are 2 Text objects that instantiate. 1 with the name and the other one with the player level. The text object with the playername works perfect except the player level. Each joined player instantiate it's one text objects. So how can I fix it the way that the Level works like the name. I tried 2 ways but without any succes. Help please
  • IBuckly
    IBuckly
    edited March 2017
    Options
    Well I figured it out a little bit, but still don't know how to put the level int in the text. The Debug log works perfect. Except the instantiated Text objects what should show the level of each player. But it shows on every instantiated prefab the same level. This is how I instantiate it.
    for (int i = 0; i < PhotonNetwork.playerList.Length; i++)
            {
                GameObject g = Instantiate(Name_List);
                g.transform.SetParent(Name_List.transform.parent);
                g.GetComponent<RectTransform>().localScale = Name_List.GetComponent<RectTransform>().localScale;
                g.GetComponent<RectTransform>().localPosition = new Vector3(Name_List.GetComponent<RectTransform>().localPosition.x, Name_List.GetComponent<RectTransform>().localPosition.y, Name_List.GetComponent<RectTransform>().localPosition.z);
                g.transform.FindChild("Player_Name").GetComponent<Text>().text = PhotonNetwork.playerList[i].name;
    
                foreach (PhotonPlayer netPlayer in PhotonNetwork.playerList)
                {
                    Debug.Log(netPlayer.name + " : " + (int)netPlayer.customProperties["lvl"]);
                    g.transform.FindChild("Player_Level").GetComponent<Text>().text = "Lvl " + (int)netPlayer.customProperties["lvl"];
                }
    
                g.SetActive(true);
                nameLists.Add(g);
            }