Saving and displaying best time per level.
Hello there, so I am working on a platformer/time-attack game that has the player trying to set and beat their best time. My timer works well, but I am trouble getting to save my best time for each level and getting it to display properly. If it's not too much to ask, could someone eyeball my code and give me a few pointers as to what I am doing wrong? Thanks in advance!
Here's my code:
#pragma strict
public static var startTime : float;
public static var endTime: float;
public var currentTimeLvel1: float = 0.0f;
//public var currentTimeLvel2: float = 0.0f;
//public var currentTimeLvel3: float = 0.0f;
//public var currentTimeLvel4: float = 0.0f;
//public var currentTimeLvel5: float = 0.0f;
public var onLevel1 : boolean = true;
//public var onLevel2 : bool = false;
//public var onLevel3 : bool = false;
//public var onLevel4 : bool = false;
//public var onLevel5 : bool = false;
private var bestTime : float = 0.0f ;
public var level;
var playTime = endTime - startTime;
public var textTime : String;
public var guiSkin : GUISkin;
function Start()
{
startTime = Time.time;
bestTime = PlayerPrefs.GetFloat("Best Time:", Mathf.Infinity) ;
Debug.Log ("Best Time:" + bestTime);
}
function Update()
{
if(onLevel1)
{
SetBestTime() ;
}
}
function SetBestTime()
{
currentTimeLvel1 = Time.time;
Debug.Log ("End Time:" + currentTimeLvel1);
if(currentTimeLvel1 < bestTime)
{
PlayerPrefs.SetFloat("Best Time:", endTime);
bestTime = endTime;
}
}
function OnGUI ()
{
var guiTime = Time.time - startTime;
var minutes : int = guiTime / 60;
var seconds : int = guiTime % 60;
var fraction : int = (guiTime * 100) % 100;
textTime = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
GUI.skin = guiSkin;
GUI.Label(Rect((Screen.width / 2) - 100, 30, 350, 80), "TIME ELAPSED: " + textTime);
GUI.Label(Rect((Screen.width / 2) - 100, 60, 350, 80), "BEST TIME: " + bestTime);
}
function LevelEnded()
{
PlayerPrefs.SetFloat("Best Time:", bestTime);
Debug.Log ("Best Time:" + bestTime);
PlayerPrefs.Save();
}
Answer by MechanicalGaming · Dec 07, 2015 at 11:30 PM
Hey there!
Try creating 2 floats. First one is for the current time of starting the scene (the time follows itself through every scene) and then make a static float. at the end of the scene (or completion) stop the static float from being equal to the time and then deduct the starting time.
We use a static float for it so that it is not forgotten and so you can reference it in other scripts. make a save script and choose to save it in that. You can then decide to round to round it up or down and change it to minutes and everything like that. Hope this helps!
Got questions? Ask away!
Here's a sample to make it easier to understand:
//Variables
public float currentTime;
public static float endTime;
//Check if complete (must put something in to confirm it)
public bool done = false;
//Start of the scene
void Start() {
currentTime = Time.time
}
void Update () {
if(done == false) {
//Update the timer
endTime = Time.time;
} else if(done == true) {
//Calculate the amount of time it took to complete the objective (Saved Variable)
endTime = endTime - currentTime;
}
}
Y'know, I was worried I wouldn't get an answer to this question of $$anonymous$$e for a few days if not ever, so first off, THAN$$anonymous$$ YOU SO $$anonymous$$UCH for your answer! Anywho, I'll rework my code a bit but I also need it to save to PlayerPrefs or an .xml to save that player's overall best time on that level so they can reopen the game and see that their previous best score for their levels are there if they want to try and beat it.
There's also the matter of getting all these times to the result screen with a GUI, any ideas on that (using PlayerPrefs or X$$anonymous$$L)?
Your welcome! And if the game you are making is a web application then I don't think there is a need to save the game. In all honesty, there is a way but I won't be able tot help you out in that matter.
But if the game you are making is a android/apple game or computer game then you can save the details easily without using any X$$anonymous$$L. Here's a video that explains how to do it : https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading The video is very long but it's worth watching!
To use stuff in the GUI you just need to reference the static variable endTime in the text. Then if you really want you could do 2 buttons. One where they can beat their score (the game ends if it goes over and it restarts for example) or they just play it casually.
Also, I've just reread your code and it seems as if you are trying to put everything into the same script. $$anonymous$$y advice would be to create a script for the text, saving, level, ect... separately. You'll get lost a little less and also it'll make the game run a little better because the script reads itself line by line so the stuff that is at the end of the script will have a short delay before being read.
If all of this works don't forget to say that make this thread an answered thread.
Your answer
Follow this Question
Related Questions
How to add one score every second to scoremanager c# 1 Answer
Looking for a simple Score Multiplier. 1 Answer
How do I add my Timer to my Score system in C# ? 0 Answers
Hey i am trying to add a time score system and time high score system need help. 0 Answers
Scoring based on count up timer 1 Answer