GML List Section of a ds_grid Based On Category?

Divinik

Member
So I have an inventory system in the game I'm programming, and I want to be able to list only a certain category selected.

The grid is set up as follows:
0 = Item Name
1 = Item Amount
2 = Item Description
3 = Item Sprite
4 = Item Script
5 = Item Type
6 = Item Icon

The code to display the full inventory is as follows:
Code:
for(i = 0; i < global.inventoryEndAt; ++i)
  {
   for(j = 0; j < global.inventoryWidth; ++j)
     {
      inventoryOnScreen = i;
        if tab = 0
          {
           if j = 0 and ds_grid_get(myItems,1,i+global.scrolledAmount) > 1{draw_text(itemLeftStart,itemTopStart + (i*24), ds_grid_get(myItems,0,i+global.scrolledAmount) +string(" x")+string(ds_grid_get(myItems,1,i+global.scrolledAmount)));}
           else {if j = 0 and ds_grid_get(myItems,1,i+global.scrolledAmount) = 1{draw_text(itemLeftStart,itemTopStart + (i*24), ds_grid_get(myItems,0,i+global.scrolledAmount));}}
           if j = 6 and ds_grid_get(myItems,1,i+global.scrolledAmount) >0  {draw_sprite(ds_grid_get(myItems,j,i+global.scrolledAmount),0,bbox_left+40,itemTopStart + (i*24)+10)}
          }
        if tab = 1
          {
          
          }
    }
  }
How would I make it so that the list only displays a certain Item Type (section 5 on the ds grid)?

Here is an image for visualization:

upload_2019-5-3_17-39-7.png
 
So firstly, you should set up enums to use instead of those numbers:
Code:
enum e_item_stats {
   NAME,
   AMOUNT,
   DESC,
   SPRITE,
   SCRIPT,
   TYPE,
   ICON
}
That way, you can reference the "section" by e_item_stats.NAME instead of 0, e_item_stats.AMOUNT instead of 1, etc. And secondly, the way I would do this is by creating a temporary grid to store the relevant items in, and then drawing that grid:
Code:
// Create Event
display_grid = ds_grid_create(7,0);
When you want the items sorted:
Code:
// Sorting the grid
ds_grid_clear(display_grid,-1);
var xx = e_item_stats.TYPE;
for (var yy=0;yy<global.inventoryEndAt;yy++) {
   if (myItems[ # xx,yy] == the_item_type_you_want) {
      var grid_height = ds_grid_height(display_grid);
      ds_grid_resize(display_grid,7,grid_height+1);
      ds_grid_set_region(display_grid,myItems,0,yy,6,yy,0,grid_height);
   }
}
Code:
// Cleanup Event
ds_grid_destroy(display_grid);
I haven't tested it, but at the end of that loop, I'm pretty sure you'll have display_grid holding the information of every item in your myItems grid that has the type "the_item_type_you_want" (obviously change this to whatever you want for the item type). Another way to do this would be to loop through the myItems grid when you want to draw it and only draw a slot when the item type is a certain value, but this way was more fun for me to make as I haven't used ds_grid_set_region before, hahaha.
 

Joe Ellis

Member
You could have separate lists for each catagory, which get put in the inventory grid, that'd be a more efficient way, cus the inventory is mainly for the player to select from, and is a grid of potentially any kind of thing, which is impossible to optimize, so rather than building the inventory grid as the root, I'd make several lists, per catagory, which then get put in the inventory grid, cus in alot of cases, it's way better to have all inventory items sorted into separate lists per type so you can quickly do certain things to each type
 
T

Timothy

Guest
ds_grids have built in functionality that will allow you to sort it by the item type. With your grid sorted like that, it's very easy to find the item type you want and draw just them because they are all next to each other.
 

Divinik

Member
Hey Refresher, would you mind showing me how to loop through and draw only the section? I can't seem to figure out how to implement the code you suggested
 
T

Taddio

Guest
but this way was more fun for me to make as I haven't used ds_grid_set_region before, hahaha.
I'm surprised, this function is real cool with the kind of game you're making. I'm thinking HOMM-style fights, you could set tiles on fire after an explosion, have spells that change the terrain (like the Swamp spell thing in homm), plant a ****load of seeds instead on 1 by 1 in the field grid, etc... This is one of the awesome function that comes with grids that sould have it's counterpart with arrays already built-in, but anyway!
 
Top