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:
and here is the script I have my trouble with:
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:
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!
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)
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
All this is visible in a little HUD image like so:
Code:
draw_sprite(s_InventoryBox, EquippedItem, o_Camera.Width-74, 10)
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!