Adding to next available slot in a grid?

Hi. I am trying to make a pick-up/inventory system. I am trying to use grids, and I am not quite sure how I am supposed to set an item to be added to the next slot in the inventory.

Here is a photo of my script; I am just adding the new widget (just contains a photo of the item) into the grid panel on the UI.

Can someone give me any logic on how to do the math to find where to place the button?

like so

I understand how to set column and row; I do not understand how to do the logic to determine which column/row slot has an object in it, or to use the next available slot.

For 100 slots: ID > 0 ? ID–;
If ID has Item then next available slot = ID+1.

what i do is i have a structure with itemData with itemID and i set the itemData in the slot widget to the items Data that way i can check if slotItemDataID = ItemID and check if its a item in it or not, if the ID is not the same and the slot itemData is not set ik there is not an item in that slot :3 you can also check if slotItemID is == 0 if it is it is empty, i just made a list with InventorySlot widgets

easiest way to do this is to make a slot array so after u add a slot also add it to the array. also add a isfull bool to your slot widget. this way any time u add a item to it u can also set the isfull bool to it also suggest adding ur item struct info to it as well for checking item ,item type ect for more complex stuff. so then when u need to check to add a item to it u can just do a foreach loop with break check to see if the slot is full if it is ignore it or what ever u need to do and if its not full u can add a new item to it and then break out of the loop.

In this picture I’m adding widgets to a grid panel in a loop. The columns variable is how many columns you want the grid to have.
The get children count is the amount of children the grid has without the widget that is to be added, hence the minus 1.

So whenever you want to add another slot, get the children count, create the widget, add the widget and set the row and column.

1 Like