# [Solved]Need Help With A Best Times List

• I am trying to put together a best times list for a game I have. This will show the shortest times to the longest times. But for some reason it isn't working when the best times are defaulted to zero. If the first element of the best scores list is a non-zero number (that is absurdly high for test purposes) it works just fine.

Any suggestions on how to do this properly? here is the code for reference.

`````` trace(Std.int(Reg.currTime));
for (i in 0...Reg.bestTimes[Reg.mazeSelect].length)
{
trace(Std.int(Reg.bestTimes[Reg.mazeSelect][i]));
//checks to see if the current playthrough's time is faster than the previous, or if the previous is set to zero
if(Std.int(Reg.bestTimes[Reg.mazeSelect][i]) > Std.int(Reg.currTime) || Std.int(Reg.bestTimes[Reg.mazeSelect][i]) == 0)
{
Reg.bestTimes[Reg.mazeSelect].insert(i, Reg.currTime);
break;
}
}
``````

I do have a working high score list but because it works in the other direction (highest to lowest), I have no problem with it.

• I decided to try a swap on the conditionals in the if statement(see below). This one puts the time in when the first listing is zero but fails to put in the new time when the first listing in not zero.

``````if(Std.int(Reg.bestTimes[Reg.mazeSelect][i]) == 0 || Std.int(Reg.bestTimes[Reg.mazeSelect][i]) > Std.int(Reg.currTime))
``````

This is starting to drive me nuts.

• I'm wondering why you are converting the values to int? Are `Reg.currTime` and `Reg.bestTimes[]` Floats? `Std.int` is like `Math.floor` removing the remainder so if < 1 it will be 0.

try

``````if (Reg.bestTimes[Reg.mazeSelect][i] == 0 || Reg.bestTimes[Reg.mazeSelect][i] > Reg.currTime)
``````

and see what you get

and maybe it's null? so you can check for that too?

``````if (Reg.bestTimes[Reg.mazeSelect][i] == null || Reg.bestTimes[Reg.mazeSelect][i] == 0 || Reg.bestTimes[Reg.mazeSelect][i] > Reg.currTime)
``````

• The casting thing was just me trying to figure it out. I wasn't casting when this bug first showed up. So my original code looked just as your suggestion does.

As for null values, I am defaulting the list to zero for each of the 10 elements. I even trace it out and it displays as 0 in the displayed trace.

• ok.

I think this

``````Reg.bestTimes[Reg.mazeSelect].insert(i, Reg.currTime);
``````

can be changed to
`Reg.bestTimes[Reg.mazeSelect][i] = Reg.currTime;`

• That wouldn't work because I need to the current element to be shifted down in the list. So if the best time was 100 seconds and the new time is 90 seconds, I need 90 in index 0 and 100 in index 1 in the final array. That is what insert does.

• Here is my code for my high scores list (high to low) for another game. It works exactly how I expect it to work.

``````for (i in 0...Reg.highScores.length)
{
if(Reg.highScores[i] < Reg.score)
{
Reg.highScores.insert(i, Reg.score);
break;
}
}
``````

• I guess this could all be moot if I were to drop the zero stuff and default my best times list to something absurd (like an hour). But I just think that looks tacky.

• what if you keep them null and test for null instead of zero? nvm, can't do that

• That actually does work. I just have to change the array type to Dynamic. Not my preferred solution, but it at least works.

• This worked for me

``````trace(Reg.currTime);
for (i in 0...Reg.bestTimes[Reg.mazeSelect].length)
{
trace(Reg.bestTimes[Reg.mazeSelect][i]);

if(Reg.currTime < Reg.bestTimes[Reg.mazeSelect][i] || Reg.bestTimes[Reg.mazeSelect][i] == 0)
{
Reg.bestTimes[Reg.mazeSelect].insert(i, Reg.currTime);
break;
}
}
trace(Reg.bestTimes[Reg.mazeSelect]);
``````

• That did it. Crazy that just switching the direction of that conditional was the solution.

Thanks a lot. I really appreciate the help.

• @ezacharyk

You should change the conditional to <= in case the values are the same or it will be skipped.

``````if(Reg.currTime <= Reg.bestTimes[Reg.mazeSelect][i] || Reg.bestTimes[Reg.mazeSelect][i] == 0)
``````

• Taking the decimal values of the float into account, the possibility of equality is quite slim. However, if they are equal, it will simply move to the next index and place it there. At least that is what should happen.

So if index 0 in the best times array is 100 and your current time is 100, it should just move on to index 1. If index 1 is 200, then the code will put the new time there and shift the other times down.

• @ezacharyk

but only if you use `<=` or it gets skipped if they're equal. no?

• @dean

Since I am not keeping track of the identity of the player who gets to time, it doesn't really matter if the resulting time is placed before or after a time in the list it is equal to.

Right now, if the current time and the time in the list are equal, it moves onto the next time in the list.

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.