Player custom properties

Hi,im new to developing in games,i have created room and i have tested for other players joining the room too(it works fine),My game is about 3d fighting game,I need help how can i set up Player who creates room as Player1 and player who joins room as Player2 ,how can i mention them in script as P1 and P2 while connecting to internet.

Comments

  • Hi @aru,

    Thank you for choosing Photon!

    You could use Player.ActorNumber of make use of PlayerNumbering script.
    Add it to the scene then call player.GetPlayerNumber();
  • aru
    aru
    Sir,this is my code where to call in the script

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using Photon.Pun;
    using Photon.Realtime;
    public class NetowrkManager : MonoBehaviourPunCallbacks
    {
    [Header("Connection Status")]
    public Text connectionStatusText;

    [Header("Login UI Panel")]
    public InputField playerNameInput;
    public GameObject Login_UI_Panel;

    [Header("Game Options UI Panel")]
    public GameObject GameOptions_UI_Panel;


    [Header("Create Room UI Panel")]
    public GameObject CreateRoom_UI_Panel;
    public InputField roomNameInputField;

    public InputField maxPlayerInputField;

    [Header("Inside Room UI Panel")]
    public GameObject InsideRoom_UI_Panel;
    public Text roomInfoText;
    public GameObject playerListPrefab;
    public GameObject playerListContent;
    public GameObject startGameButton;

    [Header("Room List UI Panel")]
    public GameObject RoomList_UI_Panel;
    public GameObject roomListEntryPrefab;
    public GameObject roomListParentGameObject;

    [Header("Join Random Room UI Panel")]
    public GameObject JoinRandomRoom_UI_Panel;

    private Dictionary<string, RoomInfo> cachedRoomList;
    private Dictionary<string, GameObject> roomListGameobjects;
    private Dictionary<int, GameObject> playerListGameobjects;

    #region Unity Methods
    void Start()
    {
    ActivatePanel(Login_UI_Panel.name);

    cachedRoomList = new Dictionary<string, RoomInfo>();
    roomListGameobjects = new Dictionary<string, GameObject>();

    PhotonNetwork.AutomaticallySyncScene = true;
    }

    void Update()
    {
    connectionStatusText.text = "Connection status:" + PhotonNetwork.NetworkClientState;
    }

    #endregion

    #region UI Callbacks
    public void OnLoginButtonClicked()
    {
    string playerName = playerNameInput.text;
    if(!string.IsNullOrEmpty(playerName))
    {
    PhotonNetwork.LocalPlayer.NickName = playerName;
    PhotonNetwork.ConnectUsingSettings();
    }
    else
    {
    Debug.Log("Playername is invalid");
    }
    }

    public void OnCreateRoomButtonClicked()
    {
    string roomName = roomNameInputField.text;

    if(string.IsNullOrEmpty(roomName))
    {
    roomName = "Room" + Random.Range(1000, 10000);
    }

    RoomOptions roomOptions = new RoomOptions();
    roomOptions.MaxPlayers = (byte)int.Parse(maxPlayerInputField.text);



    PhotonNetwork.CreateRoom(roomName,roomOptions);

    }

    public void OnCancelButtonClicked()
    {
    ActivatePanel(GameOptions_UI_Panel.name);
    }

    public void OnShowRoomListButtonClicked()
    {
    if(!PhotonNetwork.InLobby)
    {
    PhotonNetwork.JoinLobby();
    }
    ActivatePanel(RoomList_UI_Panel.name);
    }

    public void OnBackButtonClicked()
    {
    if(PhotonNetwork.InLobby)
    {
    PhotonNetwork.LeaveLobby();
    }
    ActivatePanel(GameOptions_UI_Panel.name);
    }

    public void OnLeaveButtonClicked()
    {
    PhotonNetwork.LeaveRoom();
    }

    public void OnJoinRandomRoomButtonClicked()
    {
    ActivatePanel(JoinRandomRoom_UI_Panel.name);
    PhotonNetwork.JoinRandomRoom();


    }

    public void OnStartGamebuttonClicked()
    {
    if (PhotonNetwork.IsMasterClient)
    {
    PhotonNetwork.LoadLevel("Slection Screen");
    }
    }
    #endregion


    #region Photon Callbacks
    public override void OnConnected()
    {
    Debug.Log("Connected to Internet");
    }

    public override void OnConnectedToMaster()
    {
    Debug.Log(PhotonNetwork.LocalPlayer.NickName+ " is Connected to Photon");
    ActivatePanel(GameOptions_UI_Panel.name);
    }

    public override void OnCreatedRoom()
    {
    Debug.Log(PhotonNetwork.CurrentRoom.Name + "is Created");
    }

    public override void OnJoinedRoom()
    {
    Debug.Log(PhotonNetwork.LocalPlayer.NickName + "joined to"+ PhotonNetwork.CurrentRoom.Name);
    ActivatePanel(InsideRoom_UI_Panel.name);

    if(PhotonNetwork.LocalPlayer.IsMasterClient)
    {
    startGameButton.SetActive(true);
    }
    else
    {
    startGameButton.SetActive(false);
    }

    roomInfoText.text = "Room name:" + PhotonNetwork.CurrentRoom.Name + "" +
    "Players/Max.players:" +
    PhotonNetwork.CurrentRoom.PlayerCount + "/" +
    PhotonNetwork.CurrentRoom.MaxPlayers;

    if(playerListGameobjects==null)
    {
    playerListGameobjects = new Dictionary<int, GameObject>();
    }

    //intantiating player list gameobjects
    foreach(Player player in PhotonNetwork.PlayerList)
    {
    GameObject playerListGameobject = Instantiate(playerListPrefab);
    playerListGameobject.transform.SetParent(playerListContent.transform);
    playerListGameobject.transform.localScale = Vector3.one;


    playerListGameobject.transform.Find("PlayerNameText").GetComponent<Text>().text = player.NickName;

    if(player.ActorNumber==PhotonNetwork.LocalPlayer.ActorNumber)
    {
    playerListGameobject.transform.Find("PlayerIndicator").gameObject.SetActive(true);

    }
    else
    {
    playerListGameobject.transform.Find("PlayerIndicator").gameObject.SetActive(false);

    }

    playerListGameobjects.Add(player.ActorNumber, playerListGameobject);
    }
    }

    public override void OnPlayerEnteredRoom(Player newPlayer)
    {
    //update room info text
    roomInfoText.text = "Room name:" + PhotonNetwork.CurrentRoom.Name + "" +
    "Players/Max.players:" +
    PhotonNetwork.CurrentRoom.PlayerCount + "/" +
    PhotonNetwork.CurrentRoom.MaxPlayers;


    GameObject playerListGameobject = Instantiate(playerListPrefab);
    playerListGameobject.transform.SetParent(playerListContent.transform);
    playerListGameobject.transform.localScale = Vector3.one;


    playerListGameobject.transform.Find("PlayerNameText").GetComponent<Text>().text = newPlayer.NickName;

    if (newPlayer.ActorNumber == PhotonNetwork.LocalPlayer.ActorNumber)
    {
    playerListGameobject.transform.Find("PlayerIndicator").gameObject.SetActive(true);

    }
    else
    {
    playerListGameobject.transform.Find("PlayerIndicator").gameObject.SetActive(false);

    }

    playerListGameobjects.Add(newPlayer.ActorNumber, playerListGameobject);
    }

    public override void OnPlayerLeftRoom(Player otherPlayer)
    {

    //update room info text
    roomInfoText.text = "Room name:" + PhotonNetwork.CurrentRoom.Name + "" +
    "Players/Max.players:" +
    PhotonNetwork.CurrentRoom.PlayerCount + "/" +
    PhotonNetwork.CurrentRoom.MaxPlayers;

    Destroy(playerListGameobjects[otherPlayer.ActorNumber].gameObject);
    playerListGameobjects.Remove(otherPlayer.ActorNumber);

    if(PhotonNetwork.LocalPlayer.IsMasterClient)
    {
    startGameButton.SetActive(true);
    }

    }

    public override void OnLeftRoom()
    {
    ActivatePanel(GameOptions_UI_Panel.name);

    foreach(GameObject playerListGameobject in playerListGameobjects.Values)
    {
    Destroy(playerListGameobject);
    }

    playerListGameobjects.Clear();
    playerListGameobjects = null;
    }

    public override void OnRoomListUpdate(List<RoomInfo> roomList)
    {
    ClearRoomListView();

    foreach (RoomInfo room in roomList)
    {
    Debug.Log(room.Name);

    if (!room.IsOpen || !room.IsVisible || room.RemovedFromList)
    {
    if (cachedRoomList.ContainsKey(room.Name))
    {
    cachedRoomList.Remove(room.Name);
    }
    }
    else
    {
    if(cachedRoomList.ContainsKey(room.Name))
    {
    cachedRoomList[room.Name] = room;
    }
    else
    {
    cachedRoomList.Add(room.Name, room);
    }

    }
    }

    foreach(RoomInfo room in cachedRoomList.Values)
    {
    GameObject roomListEntryGameObject = Instantiate(roomListEntryPrefab);
    roomListEntryGameObject.transform.SetParent(roomListParentGameObject.transform);
    roomListEntryGameObject.transform.localScale = Vector3.one;


    roomListEntryGameObject.transform.Find("RoomNameText").GetComponent<Text>().text = room.Name;
    roomListEntryGameObject.transform.Find("RoomPlayersText").GetComponent<Text>().text = room.PlayerCount+"/"+ room.MaxPlayers;
    roomListEntryGameObject.transform.Find("JoinRoomButton").GetComponent<Button>().onClick.AddListener(()=>OnJoinRoomButtonClicked(room.Name));

    roomListGameobjects.Add(room.Name, roomListEntryGameObject);
    }
    }

    public override void OnLeftLobby()
    {
    ClearRoomListView();
    cachedRoomList.Clear();
    }

    public override void OnJoinRandomFailed(short returnCode, string message)
    {
    Debug.Log(message);

    string roomName = "Room" + Random.Range(1000, 10000);

    RoomOptions roomOptions = new RoomOptions();
    roomOptions.MaxPlayers = 2;

    PhotonNetwork.CreateRoom(roomName, roomOptions);
    }

    #endregion

    #region Private Methods
    void OnJoinRoomButtonClicked(string _roomName)
    {
    if(PhotonNetwork.InLobby)
    {
    PhotonNetwork.LeaveLobby();
    }

    PhotonNetwork.JoinRoom(_roomName);
    }

    void ClearRoomListView()
    {
    foreach (var roomListGameobject in roomListGameobjects.Values)
    {
    Destroy(roomListGameobject);
    }

    roomListGameobjects.Clear();

    }


    #endregion

    #region Public Methods
    public void ActivatePanel(string panelToBeActivated)
    {
    Login_UI_Panel.SetActive(panelToBeActivated.Equals(Login_UI_Panel.name));
    GameOptions_UI_Panel.SetActive(panelToBeActivated.Equals(GameOptions_UI_Panel.name));
    CreateRoom_UI_Panel.SetActive(panelToBeActivated.Equals(CreateRoom_UI_Panel.name));
    InsideRoom_UI_Panel.SetActive(panelToBeActivated.Equals(InsideRoom_UI_Panel.name));
    RoomList_UI_Panel.SetActive(panelToBeActivated.Equals(RoomList_UI_Panel.name));
    JoinRandomRoom_UI_Panel.SetActive(panelToBeActivated.Equals(JoinRandomRoom_UI_Panel.name));
    }

    #endregion

    }