Add each element of the same array & get the smaller one

Hello everyone !

We are working on a racing game, and for the laps time, i’m storing them in an array, via an Add node at the end of each lap. This array is in timestamp format.

My question is, how could I add all of the times to get the race time ? Lets say I have X elements in my array (X varies from track to track), how could I make it so i get the duration of all the laps time together.

i’m sure this question is pretty dumb and is just some maths problems, but I cant figure it out.

Second question, how would I go about getting the smallest element of this array (the smallest lap time) ?

Thank you all for your help !

I have a system almost identical to what you are describing except it deals more with comparing high scores between players and a player’s own personal best time not a “lap time” but with a bit of modifications you can get a general idea of how to do this. It does involve adding a custom node to sort the “time stamp” string array but I have a tutorial for that I will link below…it is really easy to create and quite handy for these things. Also I have attached the screen shot of a general setup.

Def. misread this a bit haha I thought you were trying to organize lap times…oops. Let me help you with your actual question as well, it is actually much easier than the question I was helping you with. My advice would be to separate your time array as a “time stamp” is not very good to work with mathematically. I would have 3 arrays, 1 for the minutes, 1 for the seconds, 1 for the milliseconds (if you go that far). Then start with the millisecond array do a for each loop through all the values adding them up then divide the final by 100. You can use the modulo and get the remainder of the milliseconds to keep by itself. Add the whole integer to the seconds array and do the same thing, a for each loop and add all the seconds up then divide by 60.Again, get the modulo to keep the remainder. Finally take the whole integer add it to the minutes and then loop through the minute times and then you will have the total minutes, seconds and milliseconds as 3 separate variables. Then look at my first screen shot for a way to convert them all back to a string with a time stamp format.
**Also the float is converted to a text then string so that it rounds off the potential extra decimal places before converting to a string. Click the drop down menu on the “to text” node and you will see what I mean.

If under timestamps you mean UNIX timestamps (so integers or floats), then the solutions are quite simple concepts so I have constructed the graphs for you:

In both cases, Value will have the result in the Completed pins. Works the same with floats.

Beware, this minimum selection algorithm works only in your specific case, as timestamps are always positive, it cannot be applied as a standard solution if you have negative numbers (that needs a slight change, but in your case this solution is shorter and cleaner).

Okay, so I think the solutions are working :slight_smile: instead of using a timestamp, i’m increasing a float each milliseconds, that is then, for display purpose, transfered to Minutes:Seconds.Milliseconds format. This way, it is much more easier to store times array.
Thanks you two for your help !

Okay, everything worked perfectly fine, except that the lowest selection does not from from index 0, but from index 1 ^^’ Wich is pretty weird since it uses the exct same setup as yours. The only difference is I use a float and not an int. Does it change something ?

No, type doesn’t change anything. Maybe you accidentally start putting the laps in indexed from 1, instead of 0 (also, my code does not work with indexes, but with ForEach loop, so it should be fine even that way tho).

Yup, that was it ^^’ I’m just stupid :slight_smile: Thanks you for your help !

Glad you got it to work, have a nice day! :wink: