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
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.
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?
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): string key = string.Format("{0}{1}{2}_score", mode, difficulty, character); PlayerPrefs.SetString(key, scoreString); Where 'mode', 'difficulty', and 'character' are (presumably) integers.
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?
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): var key : string = System.String.Format("{0}{1}{2}_score", mode, difficulty, character); PlayerPrefs.SetString(key, scoreString); 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.
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'?"
Code (csharp): function Update () { var key : string = System.String.Format("{0}{1}{2}_score", Arcade.ModeInt, Arcade.DifficultyInt, Arcade.CharacterInt); PlayerPrefs.SetString(key, Stats.Score); }
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
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)'."
Code (csharp): var key : string = System.String.Format("{0}_{1}_{2}_score", Arcade.ModeInt, Arcade.DifficultyInt, Arcade.CharacterInt); PlayerPrefs.SetString(key, Stats.Score); 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.
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)?