Search Unity

Highscore playerprefs

Discussion in 'Scripting' started by karlserra, Sep 1, 2015.

  1. karlserra

    karlserra

    Joined:
    Sep 1, 2015
    Posts:
    6
    Hi guys can you help in my player prefs c# code of 5 highscore from the score that got in the game. Im just a noob in unity :). But i need help just a simple sample code that display in gui.label the highscore
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    This is really easy to figure out.
    There are lot's of examples on the internet and video portals.

    Try to get started here. There you should take a look at SetInt(), GetInt() or SetFloat() and GetFloat() respectively.

    If you can print them (that's what the examples actually do :p), you can also assign them to the text of a Label.
     
  3. karlserra

    karlserra

    Joined:
    Sep 1, 2015
    Posts:
    6
    i already done that but when i try do transfer my score to highscore it got overload something..
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You have to provide for information so that someone can help you.
     
  5. karlserra

    karlserra

    Joined:
    Sep 1, 2015
    Posts:
    6
    Its like this i try to transfer the to the value of score if its greater than the highscore
    But when i play it the unity it wont work. :(

    Void highscore(){
    Int highscore1=0;
    Int highscore2=0;
    Int highscore3=0;
    score = PlayerPrefs.GetInt ("Score", 0);
    if (score>highscore1) {
    PlayerPrefs.SetInt ("Highscore1", score);
    PlayerPrefs.Save ();
    highscore1 = PlayerPrefs.GetInt ("Highscore1",0);
    GUI.te (new Rect (275, 100, 170, 28), "1.\t"+highscore1);
    }
    Else if(score>highscore2){
    //Same as highscore1 process and also in highscore3
    }
    }
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Please use code tags.
    And I'm not sure whether you've written the code in the forum directly or copied it from MonoDevelop or Visual Studio, but there're lots of mistakes in it and it simply cannot work.

    Why do you load the current score from the player prefs? Do you save it there during the game? You locally define your highscore variables and only initialize them with 0. Hence, the score will most-likely be always higher or equal to your highscore and you'll always save the score as highscore. You have to load the highscore first, then compare it to the actual score achieved in the game and do the save logic.
     
  7. karlserra

    karlserra

    Joined:
    Sep 1, 2015
    Posts:
    6
    Sorry that you understand the code its c# code that i use in unit MonoDevelop
    i'm just trying how to override the highscore if the userscore beat it
    Code (CSharp):
    1.  
    2. //this void process when its in game over and it will place the score of the use if he/she beat the highscore(top3)
    3. Void highscore(){
    4. GUI.Box(new Rect(40, 27,(Screen.width/4) + 200,(Screen.height/4)+ 260), "HighScoreBoard");
    5. //value of the highscore1-3 and so the highscore can have value.
    6. Int highscore1=0;
    7. Int highscore2=0;
    8. Int highscore3=0;
    9. score = PlayerPrefs.GetInt ("Score", 0);
    10.  
    11. if (score>highscore1) {
    12. //i'm trying to get the value of the score because it beat the highest
    13. PlayerPrefs.SetInt ("Highscore1", score);
    14. PlayerPrefs.Save ();
    15. //to override the highscore1
    16. highscore1 = PlayerPrefs.GetInt ("Highscore1",0);
    17. // to display the code in the highscore board
    18. GUI.Label (new Rect (275, 100, 170, 28), "1.\t"+highscore1);
    19. }
    20. //Same process in highscore1 and also in highscore3
    21. Else if(score>highscore2){
    22. }
    23. }
     
  8. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Don't worry, I understand your code, but you still have wrong syntax in the script.
    First of all, it should look like:

    Code (CSharp):
    1. void Highscore()
    2. {
    3.     GUI.Box(new Rect(40, 27, (Screen.width / 4) + 200, (Screen.height / 4) + 260), "HighScoreBoard");
    4.     int highscore1 = 0;
    5.     int highscore2 = 0;
    6.     int highscore3 = 0;
    7.     score = PlayerPrefs.GetInt("Score", 0);
    8.  
    9.     if (score > highscore1)
    10.     {
    11.         PlayerPrefs.SetInt("Highscore1", score);
    12.         PlayerPrefs.Save();
    13.         highscore1 = PlayerPrefs.GetInt("Highscore1", 0);
    14.         GUI.Label(new Rect(275, 100, 170, 28), "1.\t" + highscore1);
    15.     }
    16.     else if (score > highscore2)
    17.     {
    18.         // and so on
    19.     }
    20.  
    21. }
    There are still several logical mistakes, as i've already explained in my previous post. I'm not sure whether you entirely understood what I was trying to say or not.
    You set your highscores to 0, then you compare them to your score, you write it back to your highscore.
    Next time, you simply repeat that. This cannot work as you always replace your highscore1 with the currentScore (unless it is a negative score).

    By the way, you can only call the method in OnGUI as you use GUI.XXXXX in it. You cannot call it somewhere else. OnGUI was and still is an expensive method, it may run several times per frame. Which also means, that you frequently load/save to the player prefs, many times per second! That's not good at all, one was never supposed to do more logic than necessary in the OnGUI method.

    I've written a small example on the fly, it may look more complex but it does work.
    You have to get your score from the script that handles your score.
    This example only takes the score that you can manipulate in the inspector. There's no logic that changes the score!

    First of all, load your highscores correctly.
    You should do that when your script starts (or use OnEnable instead), which handles the highscoreboard.
    I'm building the strings here in order to avoid doing that permantently in OnGUI.
    Code (CSharp):
    1. public int currentScore;
    2. private List<int> highScores = new List<int>();
    3. private List<string> highScoresAsStrings = new List<string>();
    4. private const int NUMBER_OF_SCORES = 3;
    5.  
    6. void Start()
    7. {
    8.     LoadHighScores();
    9. }
    10.  
    11. void LoadHighScores()
    12. {
    13.     for (int i = 0; i < NUMBER_OF_SCORES; i++)
    14.     {
    15.         highScores.Add(PlayerPrefs.GetInt("highscore" + (i + 1), 0));
    16.         // we build the strings here ONCE to avoid doing it frequently in the OnGUI method
    17.         highScoresAsStrings.Add(string.Format("{0}.\t{1}", i + 1, highScores[i]));
    18.     }
    19. }

    Next, display your GUI. Call this method in OnGUI.
    Code (CSharp):
    1. void DrawHighScore()
    2. {
    3.     GUI.Box(new Rect(40, 27, (Screen.width / 4) + 200, (Screen.height / 4) + 260), "HighScoreBoard");
    4.  
    5.     int scoreYpos = 100;
    6.     for (int i = 0; i < highScores.Count; i++)
    7.     {
    8.         GUI.Label(new Rect(275, scoreYpos, 170, 28), highScoresAsStrings[i]);
    9.         scoreYpos += 30;
    10.     }
    11. }
    Last but not least, save your score.
    For testing purposes (remember, this is an example, it's for you to get an idea about how to approach this) we will simulate the end of your game/round or whatever by clicking a button.

    Call DrawSaveButton in OnGUI. Ignore the positioning, it's only for testing.
    Code (CSharp):
    1. void DrawSaveButton()
    2. {
    3.     if (GUI.Button(new Rect(200, 250, 170, 25), "Save"))
    4.         SaveHighScores();
    5. }
    6.  
    7. // SaveHighScores is sometimes called in OnGUI, but only ONCE when you click the button
    8. // it's not called frequently and also shouldn't be called frequently
    9. void SaveHighScores()
    10. {
    11.     for (int i = 0; i < highScores.Count; i++)
    12.     {
    13.         if (currentScore > highScores[i])
    14.         {
    15.             // insert at the correct position
    16.             highScores.Insert(i, currentScore);
    17.             // remove the last, as it's no longer in top highscores
    18.             highScores.RemoveAt(highScores.Count - 1);
    19.             // save highscores and build new strings
    20.             for (int j = 0; j < highScoresAsStrings.Count; j++)
    21.             {
    22.                 PlayerPrefs.SetInt("highscore" + (j + 1), highScores[j]);
    23.                 highScoresAsStrings[j] = string.Format("{0}.\t{1}", j + 1, highScores[j]);
    24.             }
    25.  
    26.             // stop the loop, as we have already added the new score to the list
    27.             break;
    28.         }
    29.     }
    30. }
    I kept it pretty simple, you can do it even more structured and better. But anyway, my intention is to get you started, not to provide a perfect script for your needs.
     
    Last edited: Sep 3, 2015
  9. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Suddoha likes this.
  10. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    True.
    I always wonder why so many people still start their projects with the old GUI anyway, probably due to all the old tutorials. :/
     
    LeftyRighty likes this.
  11. karlserra

    karlserra

    Joined:
    Sep 1, 2015
    Posts:
    6
    Thank you so much :)