Help! How do i determine multiplayer racing position?

I have been stuck on this problem for a few days, any help would be appreciated!

I added a spline system, determining the position of the player along the spline.
The first attachment below shows the player (Host) is 0.16% of completing the track(spline).
The second player is the connected client showing at 0.065% of completing the track.
The variable shown in the hud is replicated through the player state

My question is how do I go about calculating the player’s position based on their percentage? For example, the player with the most percentage completed would be in 1st place and so on. I can’t seem to figure out how to compare all the connected player’s variables.

Here is a really weird way I just thought of:

Have an array of player states.

Clear it.

GameState : PlayerArray foreach outputs a playerstate on each loop until all have been treated.
On each loop, get the spline progress and multiply it by 10,000 and get the Floor of that. Use the resulting integer as an index and insert this playerstate into your playerstate array I mentioned earlier, at that index.

I know, it’s weird, but it will put the playerstates into index order in your array even though there are huge index gaps.

When the loop is done, reverse the array.

Now if you loop over that resulting playerstate array from 0 to lastindex in a for loop (not a foreach loop), skipping empty indexes, the first playerstate encountered will be the one with the most progress: first place. Second one will be second place.

I am not sure how best to handle all the gaps of indexes but instead of skippimg empty ones you can loop over that array once to copy it into another array in order. The resulting array will have no gaps.

But to do that you would have to loop.over it in order skipping gaps in the first place.
I dont know if foreach loops go in numerical index order or not ornif they skip index gaps automatically or not. Double click the node to find out I guess. :slight_smile:

This gets more complicated if you have laps because then you have to first sort the players by number of laps and then within that sorting also sort by progress along the current lap.

You dont want to have to do this every tick. Looping on a tick tends to kill performance.
So either find a better way than mine or
Do mine and whenever a player crosses a checkpoint or finish line then do this kind of check and sorting before deciding who won a very close near-tie, but for the rest of the time just rely on a timer update every half second or so.

I took part of your suggestion and got the player array from gamestate and send the player index into an array.

I then did a simple check on an event tick if host is behind connected player it would print a message on screen saying player has passed you. Im not sure if im doing this right

No, that’s not what I meant. I meant to multiply the position by 10,000, convert that to an integer, and then use that integer as the array index for storing a player state in a player state array. I know it sounds weird, but it’s the simplest way I could think of to sort the players by progress.