- Home /
Is it possible to use ToString to format a playerpref int value? (JS)
Hello everyone,
I am sorry for asking the same question again, but not too long ago where I asked if it was possible to re-format and then display a playerpref saved time. I did received an answer and at first I did thought that it was a solution to my question, however now that began to re-work into this particular code section, I find myself lost again.
I just want to know if there was any way I could re-arrange my best time score in a more readable format, for example:
right now when the player survives more than a minute it displays 67 seconds, I would like to know how I could change this to display as a 00:01:07 format.
What I am using right now to display the saved best time:
var HighscoreText : GUIText;
function Awake() {
HighscoreText.text = "Best Time: " + PlayerPrefs.GetInt ("timerScore");
}
Initially I thought that perhaps I could use the code String.Format
in the script to display the best time score.
Timer Code:
function OnGUI () {
var minutes : int = time / 60;
var seconds : int = time % 60;
var fraction : int = (time * 100) % 100;
GetComponent(GUIText).text = timertext;
timertext = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
}
However, it did not worked; I even tried substituting the String.Format
to a ToString
commend, but the same result always appeared. Though using the String.Format
does look promising and perhaps theres a way this could solve my problem?
The attempt:
Function Start () {
var minutes : int = time / 60;
var seconds : int = time % 60;
var fraction : int = (time * 100) % 100;
HighscoreText.text = "Best Score: " + PlayerPrefs.GetInt ("timerScore").ToString ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
}
So here lies my question, would it be possible to somehow tweak the ToString section in the code to make the timer best score display in a 00:00:00 format? If no, then if anyone has a solution please let me know. Additionally if any further information is required, please let me know also thanks in advance!
Complete functional code:
// 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() {
// Final Playerpref save time code
HighscoreText.text = "Best Time: " + PlayerPrefs.GetInt ("timerScore"); // at the start of the game the highscore will be displayed
}
function Start () {
HighscoreText.guiText.fontSize = 22;
}
function Update () {
if (timeActive) {
timer = Time.timeSinceLevelLoad;
time += Time.deltaTime;
}
//HighscoreText.guiText.fontSize = 18;
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;
GetComponent(GUIText).text = timertext;
timertext = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
}
function StopTimer2 () {
timer = 0;
timeActive = false;
}
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
PlayerPrefs.Save();
}
}
Your answer
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
HighScore Manage Using PlayerPrefs 1 Answer
Resticing variables to whole numbers 3 Answers
Scripting Tut. 1 Answer