Iterating through instances

I’ve decided to make an inventory using boolean variables instead of changing the animations of the inventory slots.

Once the player who is in the main scene of the game chooses a spell, a spell icon should appear in the first available slot of the spell inventory.

The inventory scene gets a global boolean value if a spell has been picked. If it is picked, start iterating through available slots, and create an icon of a chosen spell in a free slot.

Right now, the game creates the icons in all free slots, instead of only in the first free slot.

I guess that my question is, how do I properly iterate through all instances of slot.

Hi there, I do something similar in my project where the player takes food off an oven and it gets created on a free counter. I did not need to use “repeat for each” in any of my events at all with the exception of the scenario that all the counters were full and I needed to combine same food types on several counters in to one counter. So what I did was, if the bool isInUse was false, it would create food on the counter, give the food the same id as the counter, and set the bool to true. So in my project it of course chooses whatever counter it wants. Since you want the first free slot I can think of 2 ways but probably there are a lot more. You can slap an invisible sprite by the first slot and then in your condition you can say bool is true, pick all slots, in the first condition, then after that “pick slot closest to invisible sprite”. Or you can see if you can pick the slot with the lowest ID (do you need to create the ID in game? If there are just a set number of slots you can create it as an instance ID). You can get the PickingTools extension if needed for that, not sure it might have some way to do it in GDevelop as is. Anyway some thoughts for you.

Hi, thank your for responding. I can totally see how this could be done in simpler way. I’ve been following inventory game example where they use for each instance, however I decided to use booleans instead of text variable and check for collision.

Somehow I made it work so that when player picks up the spell, icons appear in order of free slots. However, I was planning on adding a possibility of dragging the spells to different slots manually after picking them up; give choice to player to rearrange them how they want. That is why I assigned ID values to each slot. Then I was thinking of iterating through them and check for their x;y; coordinates.

That way I can drag one spell icon, put it on top of the other spell icon, and they should swap places.

Currently, when I drag a spell onto an empty slot, it goes there they way its intended. However, if I drag
a spell on top of another spell, they dont swap places, instead they go to the same slot, one top of another.

This is why I think I need ID’s of the slots, so I can get their coordinates. Right now, I don’t know how to do that.

Hmm this is good planning on your part, I’m all for arranging my spells in the order i see fit. I’m heading in to work now but I can look into this more when I get home and also there are so many people here that will probably have it solved for you before I get home hehe

Click on the big plus icon at the top to pick the While event.
With a few variables, it will allow you to iterate as much as you like.
If your game hangs/crashes, you forgot to end the loop.