- Home /
How can I store highscores in PlayerPrefs?
Hey, Id like to do a local highscores for my game, so I intend to use PlayerPrefs. I have absolutely no idea where to start, I suppose how to enter your name at the end??? anyways, the size of thing Id be looking for is top 5 scores with name next to them, can someone help me with this? and explanation would be great and snippets of code (not the whole thing I want to learn it not copy! :D) would be much appreciated, thankyou.
Im using javascript or unity-script whatever its called. :)
I think it's officially called JavaScript but commonly called UnityScript, since Unitys JavaScript isn't like the web JavaScript. To resolve the ambiguity it's often called UnityScript. I might be wrong on this, but that's the impression I got reading other answers and comments.
Answer by Statement · Mar 08, 2011 at 01:38 PM
If you know you will be using 5 players;
PlayerPrefs.GetString("RecordName0", String.Empty); PlayerPrefs.GetInt("RecordScore0", -1);
PlayerPrefs.GetString("RecordName1", String.Empty); PlayerPrefs.GetInt("RecordScore1", -1);
PlayerPrefs.GetString("RecordName2", String.Empty); PlayerPrefs.GetInt("RecordScore2", -1);
PlayerPrefs.GetString("RecordName3", String.Empty); PlayerPrefs.GetInt("RecordScore3", -1);
PlayerPrefs.GetString("RecordName4", String.Empty); PlayerPrefs.GetInt("RecordScore4", -1);
The keys could be formatted String.Format("RecordName{0}", recordIndex);
and String.Format("RecordScore{0}", recordIndex);
.
Then you could provide methods similar to:
function GetScore(recordIndex : int) : int { var key = String.Format("RecordScore{0}", recordIndex); return PlayerPrefs.GetInt(key, -1); }
function SetScore(recordIndex : int, score : int) { var key = String.Format("RecordScore{0}", recordIndex); PlayerPrefs.SetInt(key, score); }
function GetName(recordIndex : int) : String { var key = String.Format("RecordName{0}", recordIndex); return PlayerPrefs.GetString(key, String.Empty); }
function SetName(recordIndex : int, name : String) { var key = String.Format("RecordScore{0}", recordIndex); PlayerPrefs.SetString(key, name); }
Then you can get the values like:
var topName = GetName(0);
var topScore = GetScore(0);
With some tinkering about I am sure you can make better, higher level methods like:
function SetRecord(score : int, name : String)
{
// Puts score and name to highscore, pushing worse score off the top 5.
// You wanted to figure out some stuff yourself :)
// But a tip is that you'd use the previous Get/Set methods.
}
may I ask, if you use player pref, where does the value being saved ?
Answer by Unamine · Mar 08, 2011 at 11:17 AM
There is a super tutorial of a full RPG Burg Zerg Arcade, which teaches that part very well, but is in C #, there is only you adapt to JavaScript, the Internet has some tutorials on how to do it ... Good Luck!
Link: http://www.burgzergarcade.com/hack-slash-rpg-unity3d-game-engine-tutorial
- Unity3D Tutorial - PlayerPrefs 1 / x
- Unity3D Tutorial - PlayerPrefs 2 / x
- Unity3D Tutorial - PlayerPrefs 3 / x
- Unity3D Tutorial - PlayerPrefs 4 / x
- Unity3D Tutorial - PlayerPrefs 5 / x
- Unity3D Tutorial - PlayerPrefs 6 / x