GML Navigating through a ds grid

MeltingCat

Member
I made an inventory system a while ago which is based on a ds grid. It all works well and now I decided to add a quick assign of my "equipped item". Basically the goal is to press a button and you will scroll through your items in the inventory without opening it up. So far I've done okay in coding everything related to the inventory but now I'm starting to doubt whether I really understand how the ds grids work.

Here is the creation of the inventory:
Code:
enum Item {
   None,
   Herb,
   Mushroom,
   Stick
//andsoon
}

//Inventory Slots
Box = ds_grid_create(Width, Height)
ds_grid_clear(Box, Item.None)

//Item Count
ItemCount = ds_grid_create(Width, Height)
ds_grid_clear(ItemCount, 0)
and here is the script I have my trouble with:

Code:
///@arg CurrentlyEquipped

var _EquippedItem = argument0

if _EquippedItem = Item.None exit
    
var _Number = 0 //Number of different items in inventory

for (var _y = 0; _y < o_Inventory.Height; _y++) {   
    for (var _x = 0; _x < o_Inventory.Width; _x++) {       
        if o_Inventory.ItemCount[# _x, _y] > 0 {
                        
            _Number ++
        }
    }
}
    //if there is more than one item in the inventory
    
if _Number > 1 {
    
    var _Slot = o_Inventory.ItemCount[# 0, 0] //defined the number of items in the next available slot
    var _SlotItem = o_Inventory.Box[# 0, 0]
    var _Found = false
    
    //find equipped item
    for (var _y = 0; _y < o_Inventory.Height; _y++) {   
        for (var _x = 0; _x < o_Inventory.Width; _x++) {       
            if o_Inventory.Box[# _x, _y] == _EquippedItem {
                
                //moves to the next available slot
                
                if _x + 1 > o_Inventory.Width {
                    _x = 0
                    if _y + 1 > o_Inventory.Height {
                        _y = 0
                    } else _y ++
                } else _x ++
                
                _Slot = o_Inventory.ItemCount[# _x, _y] //defined the number of items in the next available slot
                _SlotItem = o_Inventory.Box[# _x, _y]
                _Found = true
                
            }
            if _Found  break
        }
        if _Found break
    }
    
    //if there is no item there
    if _Slot <= 0 {
    //loop until we find an occupied slot
        while _Slot <= 0 {
        
            if _x + 1 > o_Inventory.Width {
                _x = 0
                if _y + 1 > o_Inventory.Height {
                    _y = 0
                } else _y ++
            } else _x ++
        
            _Slot = o_Inventory.ItemCount[# _x, _y]
            _SlotItem = o_Inventory.Box[# _x, _y]
            break
        }
    }
    
    o_Inventory.EquippedItem = _SlotItem
    o_Inventory.EquippedNumber = _Slot
    return true

} else return false
I believe I'm making a fundamental error in scrolling through the inventory to find the next available slot. Or there is a mistake in trying to store the _x and _y values.
All this is visible in a little HUD image like so:

Code:
draw_sprite(s_InventoryBox, EquippedItem, o_Camera.Width-74, 10)
In the sprite each frame holds the different items.

So here is what works and what doesn't:
the script works if all the items are in the same row, meaning having the same _y position. If they don't, simply nothing happens.
I've also made the same script in reverse, as in scrolling back instead of forward. But as soon as it is called I get an Error:

draw_sprite argument 2 incorrect type (undefined) expecting a Number (YYGI32)
at gml_Object_o_EquippedItem_Draw_64 (line 3) - draw_sprite(s_InventoryBox, EquippedItem, o_Camera.Width-74, 10)



Thank you for any help in advance!
 
What is this code meant to do:
Code:
if _x + 1 > o_Inventory.Width {
                   _x = 0
                   if _y + 1 > o_Inventory.Height {
                       _y = 0
                   } else _y ++
               } else _x ++
_x and _y are the loop counters. This code will add 1 to _x and _y every iteration of the loop, causing the code to skip every second row and column. Is this what you wanted?

Also, you are getting the error in reverse I believe because I assume you are doing _x --, which would put the value to -1, which is outside the range of the grid and therefore returning undefined.
 

MeltingCat

Member
I intended it to move to the next slot in the row and if that was the last one in the row to start at the first one in the next row. I thought this was only to happen after the current item was found like

Code:
if o_Inventory.Box[# _x, _y] == _EquippedItem {
But it seems that's not how it works. How would you go about in finding the slot next to the one the current item is in?
 
Top