Help - Search - Members - Calendar
Full Version: Getting The Lowest Value Of A Grid
Game Maker Community > Working with Game Maker > Advanced Users Only
Hecore
I basically need to find a method in which I can pull the smallest value out of a bunch of grids, and then go to the next smallest value and so on and so forth. I also need to know the X value of each of these numbers so I can pull the other relevant information stored in said grid.

Below is the scripts I'm trying to work on; you should be able to figure out where I'm going with this, despite not having all the information available to you.

CODE
for (a=0;a<=12;a+=1)
   {
       compare = min(estemp,pstemp[1],pstemp[2],pstemp[3])
       switch (compare)
       {
           case estemp:
               ds_list_add(turnlist,estemp)
               ds_grid_set(turnorder,a,0,estemp)
               ds_grid_set(turnorder,a,1,'spr_' + ds_grid_get(finfo,0,3))
               if enemyspeed = 0 then enemyspeed = enemyagility
               if a = 0 then cplay = 9 //Currently 9 = a foe. NEEDS WORK
               estemp += enemyagility
       ;break;
           case pstemp[1]:
               ds_list_add(turnlist,pstemp[1])
               ds_grid_set(turnorder,a,0,pstemp[1])
               ds_grid_set(turnorder,a,1,'spr_' + ds_grid_get(cplayer[0],0,0)+ 'Face')
               if pspeed[1] = 0 then pspeed[1] = pagility[1]
               pstemp[1] += pagility[1]
               if activated = 1 then
                   {
                       pstemp[1] += pagility[1] + .001
                       activated = 0
                   }
               if a = 0 then
                   {
                       cplay = 0
                       pstemp[1] += argument0
                       
                       if submenu = 3 then
                           {
                               pstemp[1] =  pstemp[(ds_grid_get(catagory,cselect-1,2)+1)]-.001
                               activated = 1
                           }
                   }
       ;break;
           case pstemp[2]:
               ds_list_add(turnlist,pstemp[2])
               ds_grid_set(turnorder,a,0,pstemp[2])
               ds_grid_set(turnorder,a,1,'spr_' + ds_grid_get(cplayer[1],0,0)+ 'Face')
               if pspeed[2] = 0 then pspeed[2] = pagility[2]
               pstemp[2] += pagility[2]
               if activated = 2 then
                   {
                       pstemp[2] += pagility[2] + .001
                       activated = 0
                   }
               if a = 0 then
                   {
                       cplay = 1
                       pstemp[2] += argument0
                   
                       if submenu = 3 then
                           {
                               pstemp[2] =  pstemp[(ds_grid_get(catagory,cselect-1,2)+1)]-.001
                               activated = 2
                           }
                   }
       ;break;
           case pstemp[3]:
               ds_list_add(turnlist,pstemp[3])
               ds_grid_set(turnorder,a,0,pstemp[3])
               ds_grid_set(turnorder,a,1,'spr_' + ds_grid_get(cplayer[2],0,0)+ 'Face')
               if pspeed[3] = 0 then pspeed[3] = pagility[3]
               pstemp[3] += pagility[3]
               if activated = 3 then
                   {
                       pstemp[3] += pagility[3] + .001
                       activated = 0
                   }
               if a = 0 then
                   {
                       cplay = 2
                       pstemp[3] += argument0
                           
                       if submenu = 3 then
                           {
                               pstemp[3] =  pstemp[(ds_grid_get(catagory,cselect-1,2)+1)]-.001
                               activated = 3
                           }
                   }
       ;break;    
       }
   }


CODE
ds_list_sort(turnlist,true)
if redraw = true
draw_rectangle_color(104,8,695,60,c_dkgray,c_dkgray,c_dkgray,c_dkgr
ay,false)
ds_list_sort(turnlist,true)
for (a=0;a<=11;a+=1)
   {
       if a = 0 then draw_rectangle_color(644,8,694,60,c_yellow,c_yellow,c_yellow,c_yell
ow,false)      draw_sprite(execute_string("sprite_index="+string(ds_grid_get(turnorder,ds_grid_value_x(turnorder,0,0,12,0,ds_list_find_value(turnlist,a)),1))),-1,646-(a*49),11)
   }
}
draw_rectangle_color(104,8,695,60,c_ltgray,c_ltgray,c_ltgray,c_ltgr
ay,true)


The method I tried to whip up works as long as all the values are different. As soon as a value is the same it pulls the first array that satisfies the condition, which won't work for me.

Any other method I could use or a good way to properly sort identical values?
Ecstats
Well I can't see anything wrong with this code. It sorts your values for you in the list. And why won't it work with it choosing the first value that is smallest, even if there are identical ones?

As a side note, this code seems overly complicated.

CODE
draw_sprite(execute_string("sprite_index="+string(ds_grid_get(turnorder,ds_grid_value_x(turnorder,0,0,12,0,ds_list_find_value(turnlist,a)),1))),-1,646-(a*49),11)


You seem to be looking for the place in the grid where the value is the same as the value above it in y. Then you use execute_string to use that value to draw a sprite. Why can't you simply use that value directly?

Oh and you call ds_list_sort(turnlist,true) twice.
Sinaz
In order to "properly" sort identical values, you need some sister value to base the "properness" on... otherwise, any sorting of identical values is arbitrary without an additional criteria.

And as a grid works... it always scans from low to high and returns the first found (I know this from tests for a destructible terrain collision system - though I haven't fully tested that with the min functions) Reversing the grid position values doesn't change anything. sad.gif

Could you describe more about what your script is for? It's a strain to read it and guess what the variables mean and what it is trying to accomplish.

Knowing what the application is would help me engineer my own solution and compare it to yours to give me an idea of what kind of advice to give.
Hecore
This is what it's for.



My game calculates the turn order of each character, and then displays it so players can get an idea of what character gets to go next. The only issue is when my characters perform special moves - they link together in the timeline in the aqua color (special moves are somewhat like the multi character attacks in Chrono Trigger). Because the players link together in the timeline my game starts suffering problems (as they perform the attack at the same time).

I'd like the display to be 100% accurate, and that's why I need the precise turn order. The display is really only for the players benefit - even if the move is visually off-target the game still works, but it's not good to have the player guessing who gets to go next.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.