Move All Items after one of the Items was used

Hi! I have a simple inventory system that looks like this


I want to implement a logic that moves all items to the left when one of them was fully used. These Inventory slots are placed in uniform grid panel so the only solution I came up with was giving each of the slots integer number based on their location and after one of the Item was used, go through a loop that finds items after the one that was used and decrement so they move a cell left. But there are rows and columns so it’s a bit harder to make this but still doable However this decision seems not optimal so I was interested if there is a more proper way to achieve my goal.

Thanks!

Consider using a Wrap Box instead of a Uni Grid - no need to play with row / colums. You’d end up with a list that looks like a grid. There would also be no need to juggle indexes at all.

Once an item has been removed, loop through the remaining widgets and (re)add them to the wrap box’ slots starting with element 0. Since a widget can only have 1 parent, it will be removed from its current container automatically.

And it does not matter if it’s in the correct slot already but you could have additional script to catch that and skip this unnecessary reparenting step - as there is a cost associated with laying out a widget in a new container.


The whole thing would also depend on how those slots are managed. Is there a fixed amount? Do empty slots at the end get removed? Or do they show empty with no item inside?


Perhaps, when the item is depleted, we can remove the entire slot rather than the occupying item. In which case a wrap box would reorder it automagically without you doing anything. And if you want to keep the empty slot visible, you can add it at the end of the wrap box - nobody will ever notice…

2 Likes

Thank you so much for your response, This is my first project and first time creating inventory so I didn’t understand all of your words, sorry! would be very happy if you could look at created logic by me and answer some of my questions, thank you so much!

Firstly I create an array with all of the inventory slots ( There is going to be a limit)


I don’t really know but while reading your answer I thought if there is any way to add inventory slots not in the beginning but on the item pickup and when there is a need for this, this would be great.

After Item was picked up it goes through the loop to find if this item already exists


If not then It finds empty one

And this is setting item to the slot

So with all the knowledge I have the only solution I thought about was after the item is used slot is going to be invisible and then create a loop to add all of the slots again checking if slot is visible. Sorry, I didn’t understand part with only 1 parent.

Speaking about slot management! There is going to be fixed amount. I want to have a background img in inventory showing all the slots, so they will always be shown but it doesn’t mean that slot itself (as a widget) is going to be visible.

Regarding last paragraph! So I can just delete slot_Widget and it will reorder itself? This would be great and I don’t even need to add it at the end because I already have empty slot background for all the items. Now that I think about it making background with empty slots is not a great idea because It won’t look so good if I want to add a scrolling mechanic but for this project I won’t have so many items :slight_smile:

But all of this will work only if there is a way to create and remove slots on demand. I only found these two functions to work with ‘Create Widget’ and ‘Remove from parent’ I haven’t tried them yet (not home) and can’t say if this will work or not but even if I can create slot on demand how do I put it in the WrapBox when creating?

Sorry if this is too much information and code to look through, I really appreciate all your help here and all of other topics I’ve seen!!!

first time creating inventory

Inventories are hard. As a rule of thumb.

disclaimer: pseudo-script below

You can absolutely create slots on demand:

And then repopulate existing slots with the remaining widgets:


It’s up to you how you want to handle it all.

  • when the last item is removed, take the empty slot and push it to the end of the wrap box:

It could look like close to this this:

The above reparents the slot to the same wrap box, placing it at the end. This way you do not need to remove the emptied slot - add it to the back instead, and the WB will reorganise the rest… Neat.

1 Like

I can’t thank you enough!!! Really! Here is what I could do with all your help!!

I’ve used second method since it is easier :sweat_smile: !

I had a problem where after Items were deleted and put to last slot, when I picked up the same Item again it was appearing in random place because references were taken from ‘Inventory slots’ array And I couldn’t use wrap box array, I tried to get around using additional integers but after some time came up with this final result!! This works fine for me in vert aspect!!! I don’t know if it’s optimal (don’t think so actually) but deleting unused slots appeal to me and I think it’s the best choice! Here is how it all looks now

I start with 0 slots and add them on demand checking if there are any available slots

After Item is depleted I call a dispatcher (didn’t even know about it) I didn’t know how to make a reference from slot to inventory since adding inventory variable to slot widget didn’t work and tried making a bridge through main character but seems that dispatchers are better :slight_smile:

So after dispatcher is called I remove slot from array and also from parent ( I don’t know if removing from parent removes it from widget fully, If it does not I’d like to know how but couldn’t find much)

That’s all! Works perfectly. Thank you so much, I learned a lot!! Could you please tell me if this code and logic is usable and not perfomance heavy?

Thanks !!!

1 Like

:slight_smile:

1 Like

It all looks more than fine to me! Congrats.

1 Like

You saved me hundreds of calculations to get where I wanted with just one word: “Wrap box”. And just: Thank you. from the bottom of my heart. Now I can sleep without feeling like a failure with codes.

1 Like