# Legacy GM[SOLVED] Laps and position system in 2D racing game

H

#### Honter

##### Guest
Hi,

I'm creating 2D, top-down view, racing game, and I'm stuck at the point where it comes to getting players position. I've already researched the topic and I now that there isn't any simple way to get player's position during the race, and that's not what I'm looking for. I thought that it will be much easier to figure out player's position at the end of each race. In order to do that, I've come up with logic that you can monitor each car's time and number of laps started, then use this equation "(number_of_laps_started) * (time)" where car with the highest number of laps (4 in this case) gets the lowest number. I know it sounds confusing, but the easiest way to explain it, will be just showing my code:

//Player car
if obj_start_line.car_player_lap == 1
{
obj_start_line.car_player_lap = 4;
}
else if obj_start_line.car_player_lap == 4
{
obj_start_line.car_player_lap = 1;
}
else if obj_start_line.car_player_lap == 2
{
obj_start_line.car_player_lap = 3;
}
else if obj_start_line.car_player_lap == 3
{
obj_start_line.car_player_lap = 2;
}

player = obj_start_line.car_player_lap *(obj_car.timer);

This code should assign the lowest number to the car which made the most laps in the shortest time (I'm not sure if it does tho)

This code repeats for every car taking part in the race, and then moves on to setting an alarm.

if (alarm[0] == -1) alarm[0] = 2 * room_speed;

And the action that is carried out when the alarm reaches 0 is:

//Calculate player's position
car_times = ds_list_create();

ds_list_sort(car_times, true);

global.player_pos = ds_list_find_index(car_times,player);

ds_list_destroy(car_times);

That's the point where I think I've made something wrong, but I don't know what. I don't have much idea about ds_list feature and I've probably used it wrong. What I intended to do, was to add all the calculated values to the list, sort it (in ascending order) and then get the index of where the player value is to figure out their position.

I think I've tried everything, and I even started 'debuging' it 'line-by-line' and drawing the variables on the screen so I can see what happens. Here are some test screenshots I've made to show you what happens.

I start the race normally, everything works fine, and when I get to the end of the race, the 'magic' begins:

(At this point, everything works fine, I'm in the middle of the last lap)

That's what happens when I get to the finish line. These lines of numbers are meant to be calculated values. Starting from the top, timer (it stops, which is good), P - means player, first value (170) represents the calculated value that is meant to go into ds_list and be used to calculate players position. Second value (1) is index in the list - position. G2 stands for green2 (green car, number 2) and P2 stands for purple2.

On this screenshot you can see that the numbers are in different order. These values change each frame, in first, player has 170 and 1 and in the next one G2 has 170 and 1 and player has 255 and 2.

Last value - for P2 is a complete mystery to me, as it's -85 and 0, and I have no idea how does it even come up.

As a result of these changes, player always ends up on 1st place, no matter what, which makes entire game a bit pointless.

And ther's also a lot of other stuff connected with it, like timer, lap counter etc. If you think that this code isn't enough to understand the logics of the entire 'laps system', I'm happy to share all of it. I just didn't want make a really long post, which would be hard to read and understand.

Also if you would like to get an executable game file, I'm happy to send it, so you can see how it goes and where the bug is.