Need help - Sorting content of Arrays

So… I’m having a bit of an issue. I’ve made an inventory system that has stacking and populates a UMG window, filling a scroll box with content from the inventory array… My issue is that the sorting of the items is based on when you pick them up, meaning that if you have two items picked up at different rates, then when you use or drop one it moves around… Let me explain with an example:

  1. Player picks up item: bandages
  2. Player picks up item: can of food
  3. Player picks up item: bandages

At this point the inventory displays:

Bandages (2)
Can of Food

If the player clicks on Bandages (2) and uses or drops one, the inventory then switches to this:

Can of Food
Bandages

I have been looking into sorting algorithms and stuff but I have absolutely no idea how to implement them. I even tried to just manually set the array index based on the item class, using a variable, but that didn’t work (guessing you can’t force multiple items ot have the same array index).

Does anyone have any idea how I would implement a sorting algorithm using blueprint? Ideally it would be alphabetical, but ANY sorting would be great - even if it was based on player pickup order, but once it was set it was maintained, etc.

I feel like it has to be easier than I am thinking - things like score boards and all that should be using this I guess? I dunno. Help :frowning:

Ehm, this shouldn’t happen by default. No offense, but this lays somewhere in your code. You will need to show us how you add, remove and display your items.
I also have an inventory system and if i just add and remove items by setting the array element directly, i don’t run into such problems (it’s C++, so it won’t help you).

Why wouldn’t it happen? It makes sense that when you remove the indexed entry and update the array that there’s a ‘gap’ in it? I’m not challenging your statement - just trying to understand :slight_smile:

I’ll get some screens sorted of the various bits and pieces to proceed :slight_smile:

This is the pickup code (the only thing missing is the “Cast to pickup” node to the left of the screenshot.

This is the code that controls the item stacking… Before stacking was added it still displayed items in the order they were picked up…

And this is the drop code… both drop and use remove the items from the array in the same method - and both cause the ordering issue…

Sorry I don’t really have the time to go through you BP, but I think eXi’s point is that an array in BP is, AFAIK, order-ensured, which means that operations on that will not change the order of the elements in them. Removing an item doesn’t change the overall order of the remaining items.

To replicate your example with Cans of Food and Bandages, I would have a Struct that is composed of two items, one being the Item in inventory and the other being the Quantity of that item. Then you store elements of that Struct in your array. If you drop an item that you have more than one of, then you just change the Quantity but don’t remove the array element. If you only have one, then you can remove the element.

Thanks for your response - and no worries about not having time to go through it :slight_smile:

I’ve been trying your idea with my current set up but it’s not working, so either I’m missing something or I’d need to completely redo it to make it work… I’ll wait and see if I get anything else useful before going down that route I think haha. Thanks though :slight_smile:

Just randomly came across this whilst looking for ways on how to sort items arrays (As arrays arent always my friend and i’m still relatively new to everything the can do) but also as this a thread that may help others, I’ll post what i have anyway.

In the example above, you have coded it so you end up doing the work for the array, you manually search everything and loop through things when you have most of the information you need already.

Instead, allow the array to do the work.

Heres what i mean:

&stc=1

What happens, is when the item is picked up, we check if the actor is stackable or not. if it isnt, we dont bother checking if we have similar items, even if they are because reasons like it may have changed to a unique or special item somehow in game for a mission/quest etc and it might be the case you change it back when its not so important and we just add it to the array.

If it is stackable, we check if that ItemID is already in there. If it doesnt find the ID, then it just gets added. if the array already contains the item, then we just increase the amount.

After the items been added, we refresh the inventory to check for any 0 amount items and remove the array item.

This is in the inventory widget, so each time an item is added or removed, it will update nicely.

&stc=1

&stc=1

Dropping an item is practically the same, you just take away from the amount rather than adding to it. Here we check if we actually can drop the item also. when we drop it, we also refresh the inventory.

&stc=1

The button index is gathered from the button when you click on it. it gets set through a variable pass through when the item widget is spawned, and is automatically updated when the inventory is refreshed. So you never need to add 10 million inventory buttons and assign it its own slot. its all automatic.

Just remember to update the index when the button has been pressed:)

Capture4.PNG&stc=1