Hello Everyone!
I’ve managed to create a simple timer and highscore system for my game (attached to a GUIText object), which indicates on how long the player is currently surviving. When the players dies the timer stops, and if the player manages to beat his/hers record, the highscore gets updated.
The script is working perfectly, however I’m having some difficulties on figuring a way on formatting the playerprefs value of the timer in a {0:00}:{1:00}:{2:00} format for the highscore.
My script is as follows:
#pragma strict
// Timer
var time : float;
var timer : int;
var timertext : String;
var timeActive : boolean;
// HighScore
var HighscoreText : GUIText;
var playername : String; // optional feature, still work in progress
function Awake() {
HighscoreText.text = "HighScore: " + PlayerPrefs.GetInt ("timerScore"); // at the start of the game the highscore will be displayed
}
function Update () {
if (timeActive) {
timer = Time.timeSinceLevelLoad;
time += Time.deltaTime;
}
HighscoreUpdate ();
}
function OnGUI () { // though ongui shouldn't be used for mobile, a simple task like this one is fine
//The gui-Time is the difference between the actual time and the start time.
var minutes : int = time / 60; //Divide the guiTime by sixty to get the minutes.
var seconds : int = time % 60;//Use the euclidean division for the seconds.
var fraction : int = (time * 100) % 100;
timertext = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
GetComponent(GUIText).text = timertext;
}
function StopTimer2 () {
timer = 0;
timeActive = false;
print ("time2 stoped!");
}
function HighscoreUpdate () {
// If the value of timer happens to be more than what the current scroe is...
if ( timer > PlayerPrefs.GetInt ( "timerScore" )) {
// then update the new score, timerScore is a playerpref created which allows the player to save his highscore
PlayerPrefs.SetInt ("timerScore", timer); // timer is the object that contains the value
}
}
As you can see im using the timertext = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction); to make the timer easier to read, rather than displaying a whole number.
The highscore function gets called every time the player dies and if a new record has been set, then it updates the value.
function HighscoreUpdate () {
// If the value of timer happens to be more than what the current scroe is...
if ( timer > PlayerPrefs.GetInt ( "timerScore" )) {
// then update the new score, timerScore is a playerpref created which allows the player to save his highscore
PlayerPrefs.SetInt ("timerScore", timer); // timer is the object that contains the value
}
}
So here’s my question, would anyone have an idea on how I could format the highscore in a way its more readable like I did for my timer? Such as using decimals points rather than having a whole number.
I was looking into playerprefs.getstring which does work to a certain extend, but it just saves whatever time the player gets after dying, so it isn’t exactly what I’m looking for.
Please let me know if I need to clarify anything else, and thanks everyone in advance!