So there are 2 variables, int Connections, and float Time, and a classification. Legendary, Epic, Excellent, and Good. I need to check to see which classification should be given. I am currently using if statements. I know there is a better way. If someone can see what my issue is, or a better way please let me know.
Using else if statements after the first if, may improve your scripts performance. That way if one of the conditions is met, then the script will not check the rest of the else if statements after the one that was found to be true.
You have a minor error in the final if statement; you’re comparing Time to GoodConnect, which I presume should be GoodTime.
I have a couple of suggestions, though “better” in this case is really just going to be a matter of preference… everything is basically going to boil down to a bunch of ifs.
You should use 'else if’s instead of the extra ‘&& !(time >= time)’. Presumably, if someone has a Legendary time, they do not also have an Epic time. Because you’re doing your comparisons from high to low, using else ifs will always get you the highest possible qualifying score category.
This lets you hide most of the implementation details away in case you want to change them later. Don’t Repeat Yourself, if possible.
Put the reference scores, in order, into a collection, then iterate (loop) over it:
CombinedScore epicScore = …
…
List referenceScoresFromHighToLow = new List()
{ legendaryScore, epicScore, excellentScore, goodScore }
foreach (CombinedScore score in referenceScoresFromHighToLow)
{
if (isBetterScore(playerScore, score))
{
classification = score.scoreName;
break;
}
}
Because this goes from High to Low, like your ifs, the loop will break out with the highest possible score that meets the requirements. You could also do this with a LINQ Find, which looks a little bit nicer but is a little bit less explicit.
First you would need to decide and explain to us how you want it to work. If “connections” is Legendary but “time” is Excellent, do we take the highest value (Legendary based on connections), the lowest (Excellent based on time) or perhaps Epic because that falls between your connection ranking and time ranking.
Without knowing the answer, something like this might simplify the problem for you (not the neatest solution but since you don’t want to use arrays etc…)
int timeRank = 0;
int connectionRank = 0;
if (curConnections >= levelIntroScript.LegendConnect)
{
connectionRank = 4;
} // use "else if" because if connections was Legendary, we don't even need to check if it was Epic
else if (curConnections >= levelIntroScript.EpicConnect)
{
connectionRank = 3;
}
else if (curConnections >= levelIntroScript.ExcellentConnect)
{
connectionRank = 2;
} // if you're supposed to always get at least "Good" rank, there's no need to compare the score
else // if (curConnections >= levelIntroScript.GoodConnect)
{
// everything less than the 3 top ranks gives a "good" rank
connectionRank = 1;
}
if (curTime <= levelIntroScript.LegendTime)
{
timeRank = 4;
} // use "else if" because if curTime was Legendary, we don't even need to check if it was Epic
else if (curTime <= levelIntroScript.EpicTime)
{
timeRank = 3;
}
else if (curTime <= levelIntroScript.ExcellentTime)
{
timeRank = 2;
} // if you're supposed to always get at least "Good" time, there's no need to compare the time
else // if (curTime <= levelIntroScript.GoodTime)
{
timeRank = 1;
}
// now you can check what ranks you got by evaluating the 2 numbers
int maxRank = Mathf.Max(timeRank, connectionRank);
int minRank = Mathf.Min(timeRank, connectionRank);
int avgRank = (timeRank + connectionRank) / 2f;