Racing Game Positioning system

I’ve been trying to figure out how to calculate race position (IE 1st, 2nd, 3rd etc.) for my game but it’s really stumping me.
I figured an easy way to do this would be to place a spline in my game that follows the track, and then get each player’s percentage along that spline and set this as a Race Progress float variable.
My code for that is here: Get Percentage Along Spline posted by anonymous | blueprintUE | PasteBin For Unreal Engine 4
This works perfectly, each racer (hoverbike) has its own unique float value based on where they are along the spline. I thought the next part would be easy too; get all of the racers in the world, then compare the RaceProgress variables and sort them into order 1 through 8 as there are 8 players total.

My code for this is here:

I can’t figure out why this isn’t working. The racers seem to be getting a random number assigned to them, even though I’m going through the vehicles one at a time and assigning them a rank starting at 1 based on the highest value in the array. As you can see here, the person in first place is listed as 3 then there’s two 0s and a 6 followed by the last in the group being 4th. It doesn’t make any sense to me.

I should say that I’m checking the RacePosition on a timer set to trigger every 0.333 seconds but of course, I only open the gate once the first check has completed to prevent the arrays from being adjusted during the calculation.

If anyone can point out what I’m doing wrong it would be greatly appreciated.

1 Like

Where are you comparing them to know in what index to place them?

1 Like

Ha ha. Maybe the situation is I don’t know what I’m doing when it comes to arrays :stuck_out_tongue:
I thought what I was doing was easier than actually comparing them - put both the bike and the RaceProgress float into their own array at the same index. This way I know which RaceProgress belongs to which bike. Then after the list is completed, I grab the index with the highest value (IE most progress down the spline) and set that as RacePosition 1 (ie 1st place). Then I remove that same index from the Bikes array so they still line up, and increment the RacePosition to the next number IE 2. This way there’s no need to compare. The code would simply get the highest value from all the values stored in the array.
Maybe the problem is that some of the bikes may have the same value for their RaceProgress if they’re tied, but still, it should grab only one result, so I’d imagine it’d just arbitrarily assign them the next 2 positions.

1 Like

Max of Float node by definition compares. My bad for not spotting it earlier.

Try this:

  1. Change the While loop for For Each that goest from array length to 0. this way you’ll assign automatically the highest value at last index, remove that from last array like you’re doing and for next loop get highest and assign it on 2 last index. This way you’ll go from high → low through the array to end up with it sorted from lowest → highest.

  2. For position, just assign the loop index + 1 and they’ll be sorted. For example: of 10 racers, loop starts with index of 9 → 1 + index = 10th position. Next loop of highest with index of 8 → 1 + index = 9th position, etc.

You don’t need this.
Screen Shot 2021-10-01 at 8.04.14 AM

Hope it helps.

1 Like

14 edits on my prev post :rofl: That’s what happens when you mess with sorting arrays while doing something else.

1 Like

Ha ha. I’ll try this tomorrow. The one thing though is because I’m using percentage along the spline, the person in first place is actually going to be the one with the highest value so I’ll take that into account. Thanks for your suggestions though, it might help me get on the right track. I’ll post my results once I get a chance to try this :slight_smile:

1 Like

(100-Percentage)-> and you’ll get 0 closet 100 farthest.

Or just do Min of Float array, not max and loop from 0 →

Oh nope. It’s calculating your progress along the spline from the first point and on a 0 - 1 scale. So zero is you just left the starting line, 0.5 means you’re half way through and 0.99 means you’ve almost finished the race, so the larger number is in first and smallest number is last. :slight_smile:
But yeah, I understand the suggestion. I really should NOT have stayed up this late watching movies and youtube :stuck_out_tongue:

1 Like

Just for the fun of it: MakeYourBets - YouTube

Oh snap! Is this the method you suggested?
I was gonna implement it today but I’m gonna pass out from exhaustion so I’ll work on it tomorrow. Well done though, there’s seriously no one online that seems to know how to achieve this. You should make a tutorial and become a God on reddit :stuck_out_tongue:


Lmfao. I’m trying to help you here. That’ll have to do.

It’s not that complicated, really. Once you figure it out keeping track of positions with multiple laps is just adding a couple of more nodes.

Oh yeah, no, I love it thank you. I really do appreciate it. I was just saying for all the other people out there too.

I think I’m missing something in my setup vs what you’ve implemented. Would you mind sharing your blueprint? This is what I’ve done based on what you said:

In that first For Each Loop I’ve got, I’m grabbing the bikes at random and simply adding them to an array. Then you’re suggesting I use a For Loop (you said For Each, but I think you meant For so I can specify the end index) to go from the last index of the race progress to the first. The problem with this is that the array isn’t sorted in any order at this point (in my code) so it wouldn’t matter what order I grab them in. Forward or backward, the Max Of Float Array is what’s doing the sorting. Then you said I don’t need the Race Position Increment, but the reason I had this is because this is how I know what position to assign the bikes. In my setup I assumed that if I’m getting the max float from the list, then I would assign that bike a value of “1” for first place, and then increment the RacePoisition so that the next highest value gets assigned a value of 2, etc. etc. So if I’m getting rid of that, I have no idea how to assign the correct race position to my bikes.
I think the reason why this doesn’t change anything with my code is because I’m not actually sorting them into an array again, I’m simply pulling them out and assigning a value.
I think your code is just built properly and mine is garbage, so if you could share what you’ve done it would really help.

Here you go:

My result:

Thank you so much! I’ll take a look at how you’ve set this up and then see if I can get it working in my project. I really appreciate the help :slight_smile:

This is so close, but it’s still not quite right. For some reason the first and last place bikes get their info wrong. Here’s my update code:

And the results:

The pink number is the ACTUAL progress of the bike independent of the sorting just so I knew it matched. It’s out by a fraction because they’re both running on their own timer.

For some reason the bike out front’s progress is displaying as 1.02 even though it’s actually at 1.9. Then the next bike is assigned 1st place as well. From here the sorting seems to be working until you factor in the bike listed as 8th. It’s place is correct based on it’s progress, but I have no idea why it’s progress is showing as negative 1 when its actual progress is 1.6

Set Array to -1 node needs to be at the end. If you change it at the start of each loop, the rest will work based on that modified value including the Max of Float. You should select highest THEN change to -1 for next loop.

I know i did length-1, but you could also simply just do a for each to have less nodes in the way. I’ve updated in the link above. In the end they both will do the same but length-1 has an extra step… just so you know.


One other thing: make all that below into a function (or custom event) in the car’s blueprint and pass the value through that, have that function update everything within when it’s called here. Less stuff around, less casting, easier to work with and iterate.

Oh, awesome thank you. That fixed everything! I actually don’t understand what that Set Array Elem node is doing.

So if I wanted to maintain the position on sequential laps, I could just add a 1 to the Race Progress right? Then 2 on the second lap etc.

Oh, yeah I’m not planning to do any of that hud stuff in the final version. It was just so I could see everything without having to use multiple text renders.

And yes, I realized that extra for loop wasn’t doing anything a few minutes after I posted :stuck_out_tongue_winking_eye: You were just using it to compare the data.

Seriously dude, thank you so much for all your help!

1 Like

It’s replacing the value with something that will always be less than any of your progress.

Yes or do something like this: Progress = (currentLapProgress+lapsCompleted)/totalLaps.

For example: 3 total laps with a racer half way → (0.5+1)/3=0.5


TorQueMoD and pezzot1, thank you guys! I’ve learned a lot from your works. Cheers!