• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Sorting grid regions

I've been at this for hours now and can't figure this out :p
I need to sort a grid in an ascending order.
The grid has 3 columns: an instance name, an instance y value, and an instance x value.

I'm doing this to have my own depth system.
Now, I definitely have to order the grid for the y values,
which is a quick
ds_grid_sort(grid, row, true)

but... some y values are the same! For those, I need to give priority to instances with a larger (or smaller) x value.
Let's say 2 instances are right next to each other, in this way, the left instance will always have priority over the right one, but this can only be done if I sort the x values as well, at least for those instances with the same y value.

A quick example:
row y row x
1 1
2 2
5 3
3 4
2 5

becomes (after sorting y)
row y row x
1 1
2 2
2 5
3 4
5 3
OR it becomes
row y row x
1 1
2 5
2 2
3 4
5 3

Notice the region where the y's are 2.
I need to make sure they always get ordered in just the one possibility:
2 2
2 5
(smaller x value gets priority)

So in order to do this, I'd need to define the region where the y values, after being sorted, are the same. Then I need to sort that region only while leaving the rest of the sorted grid intact.

Anyone have an idea?
 
[/QUOTE]
First sort the grid by x, then do the same but for y. This will give the correct order.
okay to be honest i need a little more input.
Just running the ds_grid_sort (dsgrid, 2 , true); code, followed by
ds_grid_sort(dsgrid, 1, true); code doesn't seem to work.


I need to know how to make sure the second column gets sorted first, then the first while leaving the order of the second intact. Doing one first then the second always overrides the first action, so this doesn't work.
Also this is dynamic, as the player is part of that grid. Meaning 1 "moving" value will cause the whole thing to be constantly evaluated.
 

Slyddar

Member
First sort the grid by x, then do the same but for y. This will give the correct order.
You would think it would, and it should, but on testing I discovered as well that it does not.

If you want to try it yourself, I'll save you typing it up, use my test code to generate a 30 height ds_grid and see the output.
Code:
dst = ds_grid_create(3, 30);
// 0 - name,  1 - x, 2 - y
for (var i = 0; i < ds_grid_height(dst); ++i) {
    dst[# 0, i] = "Instance" + string(i);
    dst[# 1, i] = irandom(9);    //x
    dst[# 2, i] = irandom(9);    //y
}

//sort by x first
ds_grid_sort(dst, 1, true);
//sort by y next
ds_grid_sort(dst, 2, true);

//output
for (var i = 0; i < ds_grid_height(dst); ++i) {
        show_debug_message(string(dst[# 0, i]) + ": " + string(dst[# 1, i]) + ", " + string(dst[# 2, i]))
}
 

Dmi7ry

Member
You could also expand your grid over one more column and then write in that column
y * room_width + x
And then sort that column instead.
The best solution in most number of cases (and I think this is the solution the TS should go), although may work wrong if there are objects placed outside the room.
 
Last edited:

TheouAegis

Member
The best solution in most number of cases (and I'm think this is the solution the TS should go), although may work wrong if there are objects placed outside the room.
Yeah, at least outside the room horizontally. That would cause the x value to overflow into the y value. I mean, you could modify it slightly to

Code:
var x_max = ds_grid_get_max(dsgrid,2,0,2,ds_grid_height(dsgrid)-1), x_min = ds_grid_get_min(dsgrid,2,0,2,ds_grid_height(dsgrid)-1);
//loop through all rows to add y*(x_max-x_min)+(x-x_min)
 
Top