Store held item to an empty slot in inventory

How to save an item that is being held by player to an empty slot in inventory?
It should save it in the first empty slot that is found(it have rows and column), if all slot are not empty then player can’t store the item.
The inventory is stored in structure variable.

  1. I think that I need to check every child variable.
  2. If name of a child variable is 0, then store the item there
  3. And maybe use boolean to mark that the process is completed and there is no need for the loop to countinue.

But I don’t know how to do that, I already read the example of how to use ‘for each’ here:

But my structure variable is a little bit more complex, and I don’t know how to use the ‘for each’.

I’ve never done an inventory, but I’m not sure for each is the right way to go here.
If you have a set number of pages, rows and columns, I would nest some Repeat x times events to access page/row/column[x] as explained here: Variables [GDevelop wiki]
And the loop would proceed through all the pages until an empty slot is found, in which case its coordinates would be stored until the end of the loop, and then the item would be put in it.

That makes sense, it will just be like using nested loop of i=0;i++;i<pagesCount, j=0;j++;j<rowCount, k=0;k++;k<columnCount right?

But how to pass the value of number(current x of the repetition) in the condition to the action?

Assuming your loop variable i represents the row, and j represents columns, you can access the structure with the variables as:

itemsInventory["page" + VariableString(i)]["column" + VariableString(j)]

How to get the current index of the “repeat”? And use it in the action? As number/string, I need the value of it.

You’ll need to use a variable, set it just before the repeat and update it within the repeat.

But if you use a variable, you might as well use a While event instead (as in while i < Variable(pagesCount))

1 Like