• 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!

ds_grid_sort not actually sorting?

L

lohtangclan

Guest
after a few hours of scratching my head and restarting my scripts I just have to ask here! I'm a bit new to Gamemaker, but have a little experience with C and other 3d-software-scripting languages.

I'm just trying to make sense of where and how I should be calling ds_sort_grid.
In my Create event I have the following code, which puts random values into my grid
Code:
mygrid=ds_grid_create(4,5);
for(var i=0;i<4;i++){
    for(var j=0;j<5;j++){
        val=floor(random(4));
        ds_grid_set(mygrid,i,j,val);
        }
}
I have a Draw event which displays my 4 x 5 grid -
Code:
for(i=0;i<4;i++){
    for(j=0;j<5;j++){
        draw_text(30+i*100,30+j*100,string(ds_grid_get(mygrid,i,j)));
    }
}
and finally two keypress checks in a Step event, to sort and randomise-
Code:
if(keyboard_check_pressed((ord("S")))){
        for(i=0;i<4;i++){
   
        ds_grid_sort(mygrid,i,false);
}
}

if(keyboard_check_pressed(ord("R"))){

for(var i=0;i<4;i++){
    for(var j=0;j<5;j++){
        val=floor(random(12));
        ds_grid_set(mygrid,i,j,val);
    }
    }
}
For some reason, when I hit "s" to sort the values, they are never actually sorted, but jumbled around.. And when I specify only one column for ds_grid_sort , it seems to do something to the entire grid..
When I initialise the grid to value j, and hit "s", the values seem to be sorted as I expect.. Does ds_grid have a problem with random values? Or am I doing something incredibly wrong somewhere?? (enclosed two images to show the random number example)

Ultimately I was working on a Scorched Earth/Tank Wars terrain system, but I was finding my ds_grid_sort was affecting areas of the grid that I had not specified, so went back to a much simpler test...which is driving me a little nuts!!
Thanks for any help!
run.pngnotsort.png <-images
 
Last edited by a moderator:

FrostyCat

Redemption Seeker
Your output is expected because ds_grid_sort() sorts entire rows by their value on the specified column, not just the cells in that column. So your loop sorts all rows by column 0, then by 1, then by 2, then finally by 3. And as expected, your column 3 is sorted in descending order.

If you want to sort columns without keeping the rows intact, use lists to do the sorting instead, or manually implement the sort.
 
L

lohtangclan

Guest
Many thanks for your reply...I reread it a dozen times, started to type out some "WHYYY?" stuff but then I just stared at the gamemaker documentation diagram for ds_grid_sort() again properly - I see that entire rows are being moved in relation to the column specified. *facepalm*

I'll give an array of lists a go..whew..
 
Top