- Home /
scoreboard for multiplayer game
could someone please help, Ive been scratching at this for more than two weeks and just cant figure out how to properly display an ingame scoreboard for players to view during a game. so far I've been able to spawn the entries on to the board but for some reason when a player scores the opposite player gets the points. I'm so stomped on this.. any help would be much appreciated!
I have a gameObject that works as a grid (leaderboardgrid) and the actual gameobject that gets spawned when each player joins the game (leaderboardEntry).. heresthe scripts for both:
leaderboardgrid:
public class MULeaderboardsSCORE : Photon.MonoBehaviour {
public GameObject leaderboardEntryPre;
public Transform leaderboardgrid;
public string scoreString, usernameString, facebookID = "";
public List<GameObject> entries = new List<GameObject>();
public bool GetLB;
PhotonPlayer tempPlayer;
GameObject leaderboardEntry;
void Start()
{
GetLB = true;
}
[RPC] void GetLeaderBeard(string newUsername, string newScore, string newFBimage) {
Debug.Log ("Leaderboard Updated");
leaderboardEntry = (GameObject)Instantiate (leaderboardEntryPre);
leaderboardEntry.transform.SetParent (leaderboardgrid, false);
leaderboardEntry.GetComponent<MULeaderboardEntry> ().rankString = (entries.Count + 1).ToString ();
leaderboardEntry.GetComponent<MULeaderboardEntry> ().usernameString = newUsername;
leaderboardEntry.GetComponent<MULeaderboardEntry> ().facebookID = newFBimage;
leaderboardEntry.GetComponent<MULeaderboardEntry> ().scoreString = newScore;
if (PhotonNetwork.player.customProperties ["username"].ToString () == newUsername) {
tempID = PhotonNetwork.player.ID;
leaderboardEntry.GetComponent<PhotonView> ().TransferOwnership (tempID);
Debug.Log ("Leaderboard Updated " + tempID);
}
entries.Add (leaderboardEntry);
}
[RPC] void UpdateMyScore(int MyScore){
leaderboardEntry.GetComponent<MULeaderboardEntry> ().scoreInt = MyScore;
Debug.Log (leaderboardEntry.GetComponent<MULeaderboardEntry> ().usernameString + "'s new score is " + MyScore);
}
void Update(){
if( GetLB == true){
if(PhotonNetwork.player.customProperties ["username"].ToString () == GameInformation.UserName){
tempPlayer = PhotonNetwork.player;
if (tempPlayer.customProperties ["score"].ToString () != null) {
usernameString = tempPlayer.customProperties ["username"].ToString ();
scoreString = tempPlayer.customProperties ["score"].ToString ();
facebookID = tempPlayer.customProperties ["FBImage"].ToString ();
GetComponent<PhotonView>().RPC ("GetLeaderBeard", PhotonTargets.AllBuffered, usernameString, scoreString, facebookID);
GetLB = false;
}
}
}
if (GetLB == false) {
if (scoreString != tempPlayer.customProperties ["score"].ToString ()) {
scoreString = tempPlayer.customProperties ["score"].ToString ();
int NewScoreInt = int.Parse (scoreString);
GetComponent<PhotonView> ().RPC ("UpdateMyScore", PhotonTargets.AllBuffered, NewScoreInt);
}
}
}
and the leaderboardEntry:
public class MULeaderboardEntry : Photon.MonoBehaviour {
public string rankString, usernameString, scoreString, facebookID = "";
public Text rank, username, xp;
private Texture2D tempPic;
public RawImage FBImage;
public int scoreInt;
public bool tempHS = false;
// Use this for initialization
void Start () {
rank.text = rankString;
username.text = usernameString;
if (facebookID != null) {
StartCoroutine (getFBPicture ());
Debug.Log ("Found FB ID");
} else {
facebookID = "TeamHyptown";
StartCoroutine (getFBPicture ());
Debug.Log ("Set FB ID");
}
if (scoreString != null) {
scoreInt = int.Parse (scoreString);
xp.text = scoreInt.ToString ();
Debug.Log ("Score Updated Per Entry");
tempHS = true;
}
if (photonView.isMine == true) {
gameObject.tag = "Leaderboard";
}
}
void Update(){
if (tempHS == true) {
if (xp.text != scoreInt.ToString()) {
xp.text = scoreInt.ToString();
scoreString = scoreInt.ToString();
}
}
}
public IEnumerator getFBPicture (){
var www = new WWW ("http://graph.facebook.com/" + facebookID + "/picture?width=210&height=210");
yield return www;
yield return new WaitForEndOfFrame();
Texture2D tempPic = new Texture2D (25, 25, TextureFormat.RGB24, false);
www.LoadImageIntoTexture (tempPic);
tempPic.ReadPixels(new Rect(0, 0, 25, 25), 0, 0);
tempPic.Apply();
FBImage.texture = tempPic;
}
}
Question - is this saved? Do you reset the scores of each player after the game ends, or is it like a "lifetime" leaderboard? If its not the ladder, you should use PhotonNetwork.player
for each player's scores. Its a very handy attribute of PUN. Let me know if this is the situation and i can give you an example.