I am making a game with different levels where there is a timer in each level.
So my question is, what would be the easiest way to keep track of highscores?
Like, when the timer ends in Level1, the HighscoreA variable will be equal to the timer.
And when the timer ends in Level2, the HighscoreB variable will be equal to the timer.
Are there any way where I can make the script know which Level it is in and therefore which Highscore it should change without having to make a thousind “if statements”?
I actually dont have a specifik variable telling which level the player is in, but I could easily do that. Right now the levels are changed by a UI button which calls a function with LoadLevel.
But what do you find the best way to do it?
I think that I will put a collider in the spawn of each level telling a script which level it is in
Well… since the design of how I manage which level is shown is vastly different from yours, what I would consider the best way would not fit into your design.
For your design, I’d probably just go with a dictionary… it’s simple and straight forward, and has room to grow.
public class HighScores
{
private Dictionary<string, HighscoreData> _table = new Dictionary<string, HighscoreData>();
public HighscoreData GetHighscore(string levelId)
{
HighscoreData data;
if(_table.TryGetValue(levelId, out data))
return data;
else
{
//return some sort of default highscore???
return new HighscoreData() {
LevelID = levelId,
Score = 0,
Initials = string.Empty
};
}
}
public void UpdateHighscore(HighscoreData data)
{
_table[data.LevelID] = data;
}
public struct HighscoreData
{
public string LevelID; //the level
public int Score; //the score
public string Initials; //the initials of the person, ala arcade style
//... other variables for highscore that you might include, like time, etc
}
}
Long story short
My game is about a rolling ball which mission is to complete maps and beside that do them as fast as possible.
I need some way to keep track of the highscore.
I already have a timer and a way to write it down as a highscore, however, I need a way where the same script can do it for every level where I don´t have to write something like
if (level == 2)
{
HighscoreLevel2 = something;
}
if (level == 3)
{
HighscoreLevel3 = something;
}