0
\$\begingroup\$

My in game player scoreboard UI

I've set up a scoreboard-like UI to display all players' in the room as shown in the capture above. What I'd like to do is to display each player's own status info - Life amount as of now then resources later(probably the same way when I figure this out) - to these 'Player Bar' designated to their own player.

So far each player bar doesn't function correctly and only shows the local player's status only. I took it that this is where I would want to send RPC function to do so, or correct my if that's not the way I should do it / better way of setting it up.

using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;
using Photon.Realtime;
using TMPro;

public class PlayerBarController : MonoBehaviourPunCallbacks
{
    private PlayerStats myStats;

    [Space(10)]
    [SerializeField]
    private Button portraitButton;
    [SerializeField]
    private TextMeshProUGUI nameButton;
    [SerializeField]
    private TextMeshProUGUI lifeButton;
    [SerializeField]
    private TextMeshProUGUI resourceButton;
    [SerializeField]
    private GameObject inactive;

    [Header("Explicit")]
    [SerializeField]
    private int thisActor;
    public string playerName;

    private void Start()
    {
        myStats = GameObject.Find("PersonalGameManager").GetComponent<PlayerStats>();

        CheckAvail(thisActor);
    }

    private void Update()
    {
        UpdateLife();
    }

    private void CheckAvail(int actorNumber)
    {
        Player player = PhotonNetwork.CurrentRoom.GetPlayer(actorNumber);

        if (player != null && !player.IsInactive)
        {
            inactive.SetActive(false);
        }
        else
        {
            inactive.SetActive(true);
            return;
        }
    }

    [PunRPC]
    private void UpdateLife()
    {
        //var stat = PhotonNetwork.PlayerListOthers.

        if (myStats.playerLife > 0)
        {
            lifeButton.text = myStats.playerLife.ToString();
        }
        else
        {
            lifeButton.text = "";
        }
    }
}

My earlier attempts were to use ActorNumbers to identify a unique player and access them that way but found this was quite not the way I should / wanted to do it since 1. ActorNumber's behaviour like only incrementing and not sorting in order, 2. Player already is capable of handling these data and function when players join a room. For testing reasons, I've attempted to give each player a temporary nickname; Player# 1, 2, and so on. but I'm getting PlayerNumbering indexing this, but starts from -1 where -1 was told no index(still didn't quite figure this out either since it gives the first player Player# -1, but the second player Player# 0).

using UnityEngine;
using TMPro;
using Photon.Pun;
using Photon.Realtime;
using ExitGames.Client.Photon;
using Photon.Pun.UtilityScripts;

public class WaitingRoomPlayerListing : MonoBehaviourPunCallbacks
{
    [SerializeField]
    private TextMeshProUGUI _text;

    public Player player { get; private set; }
    public bool ready = false;

    private void Start()
    {
        PlayerNumbering.OnPlayerNumberingChanged += UpdateNumberingIndex;
    }

    public void SetPlayerInfo(Player pp)
    {
        player = pp;
        SetPlayerText(pp);
    }

    public override void OnPlayerPropertiesUpdate(Player targetPlayer, Hashtable changedProps)
    {
        base.OnPlayerPropertiesUpdate(targetPlayer, changedProps);
        if (targetPlayer != null && targetPlayer == player)
        {
            if (changedProps.ContainsKey("RandomNumber"))
            {
                SetPlayerText(targetPlayer);
            }
        }
    }

    private void SetPlayerText(Player pp)
    {
        int result = -1;

        if (player.CustomProperties.ContainsKey("RandomNumber"))
        {
            result = (int)pp.CustomProperties["RandomNumber"];
        }

        if (pp.NickName == string.Empty)
        {
            _text.text = result.ToString() + ", " + "Player# " + pp.GetPlayerNumber().ToString();
        }
        else
        {
            _text.text = result.ToString() + ", " + pp.NickName;
        }
    }

    private void UpdateNumberingIndex()
    {
        player.GetPlayerNumber();
        Debug.Log(player.GetPlayerNumber().ToString());
    }
}

There are more things I'd like to do where this would lead up to very soon(mechanics like sending message to another player on click or switch to view another player's camera on click), but I think I should figure this out beforehand. This is my very first multiplayer game, so pardon me if some setup concepts are weird or wrong.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.