scoreboard posting scores on the wrong player.. Please Help

Options
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;
     }
 }

is there any tutorials out there demonstrating a simple in-game scoreboard??? there has to be someone out there thatsdone something similar for maybe an fps type game or anything... :oops:

Comments

  • dontonka
    Options
    Hello Sir Damus.

    So there are some stuff which looks buggy in your code :roll:, but let me pin point to the main problem you are describing :D. The problem is that in the UpdateMyScore method, you are assuming leaderboardEntry is pointing to the entry of the player that needs to be score updated (or tempPlayer as you refer to it), which might not be true, as GetLeaderBeard will gets called by all the players, and leaderboardEntry will point basically to the latest player that joined the game, which might not be that player.

    Solution:

    [code2=csharp]// You need to pass a new parameter indicating the score is from which player!!
    [RPC] void UpdateMyScore(String username, int MyScore){
    // Go over the entries list, which has all the player entry.
    // In that loop, check which every entry username, and when it match the one received in parameter, you got it, you do the same as before..
    foreach(...)
    {
    if (leaderboardEntryFromLoop.GetComponent<MULeaderboardEntry> ().usernameString.Equals(username) {
    leaderboardEntryFromLoop.GetComponent<MULeaderboardEntry> ().scoreInt = MyScore;
    Debug.Log (leaderboardEntryFromLoop.GetComponent<MULeaderboardEntry> ().usernameString + "'s new score is " + MyScore);
    break;
    }
    }
    }[/code2]

    Hope this helps a bit :D, and let me know how it result :).

    Cheers,
    Don T.
  • naltradamus
    Options
    ooooo ok! thank you so much Don!! i was getting so stressed out tryna figure out what was missing. you said you noticed some other bugs in my script? im all ears if you have any suggestions..

    thanks,
    -CJ
  • naltradamus
    Options
    it worked perfectly Don!! thanks!! im so happy to finally get the progress going agian
  • dontonka
    Options
    Hello CJ,

    I'm happy for you, than it was worth spending 10 min. to understand your code :D.

    The other problem I was referring, which at the end might not be a problem directly, is that I think the code in the Start method from MULeaderboardEntry is garbage at the end, has this is executed when you do "leaderboardEntry = (GameObject)Instantiate (leaderboardEntryPre);" and at that point your variablse inside MULeaderboardEntry instance are empty anyways, except facebookID.

    The only thing that might work there is this since you initialize facebookID to "".

    [code2=csharp]if (facebookID != null) {
    StartCoroutine (getFBPicture ());
    Debug.Log ("Found FB ID");[/code2]

    Good luck man.

    Cheers,
    Don T.
  • naltradamus
    Options
    thanks don!
    finally had some time today to clean things up a bit and things still work perfectly! do you have any tips on keeping my scoreboard in the correct order (from highest score to lowest score) during game play?
    I want my players to have the ability to see whos winning during runtime..

    here is what i have so far (Excuse the messy coding, Ive been shuffling things around to see what works) It kind of works 90% of the time.. the ranking number doesnt always update correctly when there's 3 or more players..
    public class MULeaderboardsSCORE : Photon.MonoBehaviour {
    	
    	public GameObject leaderboardEntryPre;
    	public Transform leaderboardgrid;
    	public string scoreString, usernameString, facebookID = "";
    	public List&lt;GameObject&gt; entries = new List&lt;GameObject&gt;();
    	public int FirstPlaceScore = 99, SecondPlaceScore = 99, ThirdPlaceScore = 99, NoPlaceScore = 0, tempID, LeaderboardIndex = 99;
    	public bool GetLB;
    	PhotonPlayer tempPlayer;
    	GameObject leaderboardEntry;
    
    	void Start()
    	{
    		GetLB = true;
    	}
    
    	void Update(){
    
    		if (GetLB == true) {
    				 
    				
    			if (PhotonNetwork.player.customProperties &#91;"username"&#93;.ToString () == GameInformation.UserName) {
    				tempPlayer = PhotonNetwork.player;
    				if (tempPlayer.customProperties &#91;"score"&#93;.ToString () != null) {
    					usernameString = tempPlayer.customProperties &#91;"username"&#93;.ToString ();
    					scoreString = tempPlayer.customProperties &#91;"score"&#93;.ToString ();
    					facebookID = tempPlayer.customProperties &#91;"FBImage"&#93;.ToString ();
    					GetComponent&lt;PhotonView&gt; ().RPC ("GetLeaderBeard", PhotonTargets.AllBuffered, usernameString, scoreString, facebookID);
    					GetLB = false;
    				}
    			}
    		}
    
    		if (GetLB == false) {
    
    			if (scoreString != tempPlayer.customProperties &#91;"score"&#93;.ToString ()) {
    				scoreString = tempPlayer.customProperties &#91;"score"&#93;.ToString ();
    				int NewScoreInt = int.Parse (scoreString);
    				GetComponent&lt;PhotonView&gt; ().RPC ("UpdateMyScore", PhotonTargets.AllBuffered, usernameString, NewScoreInt);
    			}
    		
    			
    			foreach (GameObject original in entries) {
    				if (original.GetComponent&lt;MULeaderboardEntry&gt; ().usernameString.Equals (usernameString)) {
    
    					if (original.GetComponent&lt;MULeaderboardEntry&gt; ().scoreInt &gt; FirstPlaceScore) {
    						int newFirstPlaceScore = original.GetComponent&lt;MULeaderboardEntry&gt; ().scoreInt;
    						LeaderboardIndex = 0;
    						GetComponent&lt;PhotonView&gt; ().RPC ("UpdateLeaderboardPlacing1", PhotonTargets.AllViaServer, LeaderboardIndex, usernameString, newFirstPlaceScore);
    					}
    					if (original.GetComponent&lt;MULeaderboardEntry&gt; ().scoreInt == FirstPlaceScore && LeaderboardIndex &gt; 0) {
    						if (original.GetComponent&lt;MULeaderboardEntry&gt; ().scoreInt &gt; SecondPlaceScore) {
    							int newSecondPlaceScore = original.GetComponent&lt;MULeaderboardEntry&gt; ().scoreInt;
    							LeaderboardIndex = 1;
    							GetComponent&lt;PhotonView&gt; ().RPC ("UpdateLeaderboardPlacing2", PhotonTargets.AllViaServer, LeaderboardIndex, usernameString, newSecondPlaceScore);
    						}
    					}
    					if (original.GetComponent&lt;MULeaderboardEntry&gt; ().scoreInt &lt; FirstPlaceScore) {
    						if (original.GetComponent&lt;MULeaderboardEntry&gt; ().scoreInt &gt; SecondPlaceScore) {
    							int newSecondPlaceScore = original.GetComponent&lt;MULeaderboardEntry&gt; ().scoreInt;
    							LeaderboardIndex = 1;
    							GetComponent&lt;PhotonView&gt; ().RPC ("UpdateLeaderboardPlacing2", PhotonTargets.AllViaServer, LeaderboardIndex, usernameString, newSecondPlaceScore);
    						}
    					}
    
    					if (original.GetComponent&lt;MULeaderboardEntry&gt; ().scoreInt == SecondPlaceScore && LeaderboardIndex &gt; 1) {
    						if (original.GetComponent&lt;MULeaderboardEntry&gt; ().scoreInt &gt; ThirdPlaceScore) {
    							int newThirdPlaceScore = original.GetComponent&lt;MULeaderboardEntry&gt; ().scoreInt;
    							LeaderboardIndex = 2;
    							GetComponent&lt;PhotonView&gt; ().RPC ("UpdateLeaderboardPlacing3", PhotonTargets.AllViaServer, LeaderboardIndex, usernameString, newThirdPlaceScore);
    						}
    					}
    					if (original.GetComponent&lt;MULeaderboardEntry&gt; ().scoreInt &lt; SecondPlaceScore) {
    						if (original.GetComponent&lt;MULeaderboardEntry&gt; ().scoreInt &gt; ThirdPlaceScore) {
    							int newThirdPlaceScore = original.GetComponent&lt;MULeaderboardEntry&gt; ().scoreInt;
    							LeaderboardIndex = 2;
    							GetComponent&lt;PhotonView&gt; ().RPC ("UpdateLeaderboardPlacing3", PhotonTargets.AllViaServer, LeaderboardIndex, usernameString, newThirdPlaceScore);
    						}
    					}
    
    					if (original.GetComponent&lt;MULeaderboardEntry&gt; ().scoreInt == ThirdPlaceScore && LeaderboardIndex &gt; 2) {
    						if (original.GetComponent&lt;MULeaderboardEntry&gt; ().scoreInt &gt; NoPlaceScore) {
    							int newNoPlaceScore = original.GetComponent&lt;MULeaderboardEntry&gt; ().scoreInt;
    							LeaderboardIndex = 3;
    							GetComponent&lt;PhotonView&gt; ().RPC ("UpdateLeaderboardPlacing4", PhotonTargets.AllViaServer, LeaderboardIndex, usernameString, newNoPlaceScore);
    						}
    					}
    					if (original.GetComponent&lt;MULeaderboardEntry&gt; ().scoreInt &lt; ThirdPlaceScore) {
    						if (LeaderboardIndex != 3) {
    							int newNoPlaceScore = original.GetComponent&lt;MULeaderboardEntry&gt; ().scoreInt;
    							LeaderboardIndex = 3;
    							GetComponent&lt;PhotonView&gt; ().RPC ("UpdateLeaderboardPlacing4", PhotonTargets.AllViaServer, LeaderboardIndex, usernameString, newNoPlaceScore);
    						}
    					}
    				}
    			}
    		}
    	}
    
    	&#91;RPC&#93; void UpdateLeaderboardPlacing1(int transform1, string username1, int MyScore1){
    		foreach (GameObject leaderboardEntry1 in entries) 
    		{
    			if (leaderboardEntry1.GetComponent&lt;MULeaderboardEntry&gt; ().usernameString.Equals (username1)) {
    				leaderboardEntry1.GetComponent&lt;MULeaderboardEntry&gt; ().rankString = (transform1 + 1).ToString();
    				leaderboardEntry1.transform.SetSiblingIndex (transform1);
    				FirstPlaceScore = MyScore1;
    				Debug.Log (leaderboardEntry1.GetComponent&lt;MULeaderboardEntry&gt; ().usernameString + " has 1st Placing On LeaderBoard");
    				break;
    
    			}
    		}
    	}
    	&#91;RPC&#93; void UpdateLeaderboardPlacing2(int transform2, string username2, int MyScore2){
    		foreach (GameObject leaderboardEntry2 in entries) 
    		{
    			if (leaderboardEntry2.GetComponent&lt;MULeaderboardEntry&gt; ().usernameString.Equals (username2)) {
    				leaderboardEntry2.GetComponent&lt;MULeaderboardEntry&gt; ().rankString = (transform2 + 1).ToString();
    				leaderboardEntry2.transform.SetSiblingIndex (transform2);
    				SecondPlaceScore = MyScore2;
    				Debug.Log (leaderboardEntry2.GetComponent&lt;MULeaderboardEntry&gt; ().usernameString + " has 2nd Placing On LeaderBoard");
    				break;
    				
    			}
    		}
    	}
    	&#91;RPC&#93; void UpdateLeaderboardPlacing3(int transform3, string username3, int MyScore3){
    		foreach (GameObject leaderboardEntry3 in entries) 
    		{
    			if (leaderboardEntry3.GetComponent&lt;MULeaderboardEntry&gt; ().usernameString.Equals (username3)) {
    				leaderboardEntry3.GetComponent&lt;MULeaderboardEntry&gt; ().rankString = (transform3 + 1).ToString();
    				leaderboardEntry3.transform.SetSiblingIndex (transform3);
    				ThirdPlaceScore = MyScore3;
    				Debug.Log (leaderboardEntry3.GetComponent&lt;MULeaderboardEntry&gt; ().usernameString + " has 3rd Placing On LeaderBoard");
    				break;
    				
    			}
    		}
    	}
    	&#91;RPC&#93; void UpdateLeaderboardPlacing4(int transform4, string username4, int MyScore4){
    		foreach (GameObject leaderboardEntry4 in entries) 
    		{
    			if (leaderboardEntry4.GetComponent&lt;MULeaderboardEntry&gt; ().usernameString.Equals (username4)) {
    				leaderboardEntry4.GetComponent&lt;MULeaderboardEntry&gt; ().rankString = "-";
    				leaderboardEntry4.transform.SetSiblingIndex (transform4);
    				NoPlaceScore = MyScore4;
    				Debug.Log (leaderboardEntry4.GetComponent&lt;MULeaderboardEntry&gt; ().usernameString + " has No Placing On LeaderBoard");
    				break;
    				
    			}
    		}
    	}
    	&#91;RPC&#93; void GetLeaderBeard(string newUsername, string newScore, string newFBimage) {
    		
    		Debug.Log ("Leaderboard Updated");
    		leaderboardEntry = (GameObject)Instantiate (leaderboardEntryPre);
    		leaderboardEntry.transform.SetParent (leaderboardgrid, false);
    		leaderboardEntry.GetComponent&lt;MULeaderboardEntry&gt; ().rankString = (entries.Count + 1).ToString ();
    		leaderboardEntry.GetComponent&lt;MULeaderboardEntry&gt; ().usernameString = newUsername;
    		leaderboardEntry.GetComponent&lt;MULeaderboardEntry&gt; ().facebookID = newFBimage;
    		leaderboardEntry.GetComponent&lt;MULeaderboardEntry&gt; ().scoreString = newScore;
    		entries.Add (leaderboardEntry);
    	}
    	&#91;RPC&#93; void UpdateMyScore(string username, int MyScore){
    		foreach(GameObject leaderboardEntryFromLoop in entries)
    		{
    			if (leaderboardEntryFromLoop.GetComponent&lt;MULeaderboardEntry&gt; ().usernameString.Equals(username)) {
    				leaderboardEntryFromLoop.GetComponent&lt;MULeaderboardEntry&gt; ().scoreInt = MyScore;
    				Debug.Log (leaderboardEntryFromLoop.GetComponent&lt;MULeaderboardEntry&gt; ().usernameString + "'s new score is " + MyScore);      
    				break;
    			}
    		}
    	}
    }
    
  • dontonka
    Options
    Hello Damus,

    So basically, this is way too complex and messy :wink:. From conceptual logic, the best way todo that would be to refresh the order when the score get updated, that's it.

    Since UpdateMyScore is called every time a player socre change, on all the instances, the only thing you would need todo is do another loop following the actual for loop from that method, and refresh the position of every player according to their current score.

    Also, by doing that, you can get rid of all UpdateLeaderboardPlacing* methods, and also that loop into the Update method (foreach (GameObject original in entries) ).

    Hope this helps. I will not write the code and let you work a bit, and also learn more that way :).

    Regards,
    Don T.

  • naltradamus
    Options
    wow!! thanks so much Don! as you can see, i am a bit of an over thinker :D:D
  • dontonka
    Options

    :Dthat's fine bro

    good luck!