Search Unity

PlayerPrefs question?

Discussion in 'Scripting' started by Deleted User, Dec 16, 2010.

  1. Deleted User

    Deleted User

    Guest

    Hello(I hope this question make sense).

    In my game, the player first selects from several options (mode, difficulty, character, etc.) using toolbars (one toolbar for each option). Depending on the options different scripts are loaded in the next scene (but its always the same scene). After the game is over, the player goes to the "Over" scene (again, its always the same), which displays their score (just calling the static variable "Score") and a leaderboard of their previous scores (which I dont have implemented yet).

    I want each combination of options to have its own score leaderboard, how would I do that? Is there a way to reference the combination of variables from the options menu script in the playerprefs script thats on the main menu (so that each time there is a new combination, playerprefs just sets a new integer and if that combination has been used before it loads the existing scores)?

    Thanks
     
  2. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    I imagine you'd just have to implement this yourself using some sort of encoding system. Just as an example, if there are two modes, three difficulty levels, and five characters, you could prefix the key for each score or set of scores (e.g. saved as a string) with a three-digit code indicating the mode, difficulty level, and character. (So for example, the key for mode 0, difficulty level 1, and character 0 would be '010_score'.)

    Maybe there are better solutions, but that's what first comes to mind.
     
  3. Deleted User

    Deleted User

    Guest

    Would I have to write a different script for each option combination? If its not too much trouble, can you please show me an example of this in PlayerPrefs?
     
  4. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    No, you wouldn't have to write a separate script for each combination. You would simply build the key as needed based on the options, e.g. (untested):

    Code (csharp):
    1. string key = string.Format("{0}{1}{2}_score", mode, difficulty, character);
    2. PlayerPrefs.SetString(key, scoreString);
    Where 'mode', 'difficulty', and 'character' are (presumably) integers.
     
  5. Deleted User

    Deleted User

    Guest

    Can mode, difficulty and character be static variables in other scripts?
     
  6. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    As long as the code compiles and works correctly, they can be whatever you want them to be :)
     
  7. Deleted User

    Deleted User

    Guest

    I put in the script and I get the error "Assets/NewBehaviourScript 1.js(2,27): UCE0001: ';' expected. Insert a semicolon at the end."
    I fix that, and then I get errors about string and scorestring being unknown identifiers, do I need to define them as variables first?
     
  8. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Jesse Ander's sample code is in C#, you need to translate it to JavaScript first.

    EDIT: balls, I know it's going to happen anyway:
    Code (csharp):
    1.  
    2. var key : string = System.String.Format("{0}{1}{2}_score", mode, difficulty, character);
    3. PlayerPrefs.SetString(key, scoreString);
    4.  
    EDITx2: Perhaps I misunderstood. "scoreString" should be whatever data you want to store for that score. I think System.String.Format will solve the other error.
     
  9. Deleted User

    Deleted User

    Guest

    I put his code in a C# script, and I get errors.
     
  10. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Do you have a question?
     
  11. Deleted User

    Deleted User

    Guest

    FizixMan,
    Im sorry, but I get still get an error
    "Assets/NewBehaviourScript.js(3,19): BCE0018: The name 'string' does not denote a valid type ('not found'). Did you mean 'System.Runtime.CompilerServices.StrongBox'?"
     
    Last edited by a moderator: Dec 18, 2010
  12. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Post your full code.
     
  13. Deleted User

    Deleted User

    Guest

    Code (csharp):
    1. function Update () {
    2.     var key : string = System.String.Format("{0}{1}{2}_score", Arcade.ModeInt, Arcade.DifficultyInt, Arcade.CharacterInt);
    3. PlayerPrefs.SetString(key, Stats.Score);
    4. }
     
  14. afalk

    afalk

    Joined:
    Jun 21, 2010
    Posts:
    164
    A couple thoughts :: 1st --> You have to give your script a meaningful name not NewBehaviourScript (esp. for c# scripts), oh and if you use the C# version make sure to save it as .cs of course :)
     
  15. Deleted User

    Deleted User

    Guest

    I am only using NewBehaviourScript as a temp. name, and what is wrong with the JS version?
     
  16. Deleted User

    Deleted User

    Guest

    Okay, for that last error I changed string to String, and that error is gone, but now I get this
    "Assets/NewBehaviourScript.js(4,30): BCE0017: The best overload for the method 'UnityEngine.PlayerPrefs.SetString(String, String)' is not compatible with the argument list '(String, int)'."
     
  17. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    If Stats.Score is an integer, then you'll want to use SetInt() rather than SetString().
     
  18. callahan.44

    callahan.44

    Joined:
    Jan 9, 2010
    Posts:
    694
    Code (csharp):
    1.  
    2. var key : string = System.String.Format("{0}_{1}_{2}_score", Arcade.ModeInt, Arcade.DifficultyInt, Arcade.CharacterInt);
    3. PlayerPrefs.SetString(key, Stats.Score);
    4.  
    Personally, I'd extra "_" so you can get the numbers back easier with String.Split('_');
    Not sure you'd want it in Update(). It just needs to be in a function when you want to save a new score.
     
  19. Deleted User

    Deleted User

    Guest

    Thanks everyone for all of your help. Is there any way to store/retrieve multiple versions of the same intiger (for a local high score table)?
     
    Last edited by a moderator: Dec 19, 2010