Sorting objects by positions.

Hello everyone!
I am creating a little race game where 4 objects are racing each other. I want to give each object its own place (1st, 2nd, 3rd & 4th) depending on their actual position. But I've been strugglin for a while now. I think that I can do it by creating a ds_list and sorting it by their position, but I got really confused right now I can't think properly.

Do you suggest solving the problem by using other methods?

Thanks for your time =)
 

PlayerOne

Member
While a ds_list would be simple it would best to use a ds_grid or 2d array. That way you can save time, pole position, and any other stats when a race ends.

Code:
EXAMPLE 2D ARRAY:

//Player1
data[0,0]=Time
data[0,1]=Position
data[0,2]=Etc

//Player2
data[1,0]=Time
data[1,1]=Position
data[1,2]=Etc

//Player3
data[2,0]=Time
data[2,1]=Position
data[2,2]=Etc

//Player4
data[3,0]=Time
data[3,1]=Position
data[3,2]=Etc
 

2Dcube

Member
How are you supposed to update the order with a ds_grid\2d array? Manually copy all data?
I think using a list of maps is easier to work with in the end.

Create
Code:
players = ds_list_create();
To add a player:
Code:
var player = ds_map_create();
player[? "name"] = "whatever";
player[? "x"] = 0;
player[? "time"] = 0;
ds_list_add(players, player);
To get a sorted list of player positions (sorted by x position), use the following code:
Code:
players_sorted = get_sorted_players();
Which uses this script:
Code:
///get_sorted_players()

var prio = ds_priority_create();
for(var i=0; i<ds_list_size(players); i++)
{
  player = players[| i];
  ds_priority_add(prio, player, player[? "x"]);
}
var sorted_list = ds_list_create();
while(ds_priority_size(prio) > 0)
{
  ds_list_add(sorted_list, ds_prioriy_find_max(prio));
  ds_priority_delete_max(prio);
}
ds_priority_destroy(prio);
return sorted_list;
 
While a ds_list would be simple it would best to use a ds_grid or 2d array. That way you can save time, pole position, and any other stats when a race ends.

Code:
EXAMPLE 2D ARRAY:

//Player1
data[0,0]=Time
data[0,1]=Position
data[0,2]=Etc

//Player2
data[1,0]=Time
data[1,1]=Position
data[1,2]=Etc

//Player3
data[2,0]=Time
data[2,1]=Position
data[2,2]=Etc

//Player4
data[3,0]=Time
data[3,1]=Position
data[3,2]=Etc
Thanks for your input!
I'll try as soon as I can to implement 2D arrays.
 
How are you supposed to update the order with a ds_grid\2d array? Manually copy all data?
I think using a list of maps is easier to work with in the end.

Create
Code:
players = ds_list_create();
To add a player:
Code:
var player = ds_map_create();
player[? "name"] = "whatever";
player[? "x"] = 0;
player[? "time"] = 0;
ds_list_add(players, player);
To get a sorted list of player positions (sorted by x position), use the following code:
Code:
players_sorted = get_sorted_players();
Which uses this script:
Code:
///get_sorted_players()

var prio = ds_priority_create();
for(var i=0; i<ds_list_size(players); i++)
{
  player = players[| i];
  ds_priority_add(prio, player, player[? "x"]);
}
var sorted_list = ds_list_create();
while(ds_priority_size(prio) > 0)
{
  ds_list_add(sorted_list, ds_prioriy_find_max(prio));
  ds_priority_delete_max(prio);
}
ds_priority_destroy(prio);
return sorted_list;
Wow! I appreciate it a lot. I wouldn't figure this on my own.

I have a question.

Once I get the sorted list back, I can look for the instance's ids using a for and setting the corresponding position to each object, right?
 

2Dcube

Member
Yes, you could do this:
Code:
for(var i=0; i<ds_list_size(players_sorted); i++)
{
  player = players_sorted[| i];
  player[? "position"] = i; // or use i + 1 if you want "position" to start from 1.
}
I realize now you could probably just sort the original players list, in stead of keeping a separate sorted list.
Just with
players = get_sorted_players();
 
Yes, you could do this:
Code:
for(var i=0; i<ds_list_size(players_sorted); i++)
{
  player = players_sorted[| i];
  player[? "position"] = i; // or use i + 1 if you want "position" to start from 1.
}
I realize now you could probably just sort the original players list, in stead of keeping a separate sorted list.
Just with
players = get_sorted_players();
Thank you so much !

I'll go now and learn more about data structures to fully undesrtand it. I get most of what you type but I'll get it in the end.
 
Isn't the hard part of this problem, not sorting the list, but determining where the racers are in the first place? Unless you've got a completely straight track, you'll need to take into account the curves and how they change the position rank.
 
Top