Grid Inventory sort function

I am making a grid inventory similar to tarkov based on [Open Source] Grid Based Inventory Example Im stuck on a function that lets you sort the item into a spot, like when you pick up a item it automatically sorts itself, or just to make the tools inside the inventory auto sort themselfs as soon as you load in. I am really stuck and Im wondering if theres a way to implement it.

1 Like

Tough problem.

My solution would be to store the inventory as a grid and use the grid itself as a physical representation of your inventory space. Then, to find if there is space for an m x n item, just iterate over all possible m x n areas available in your inventory. Go from left to right, moving downwards as you pass each row, and store the first result as the occupying space.

Quite an inefficient implementation, however. It shouldn’t be too bad, as at most you’d have to do width x height loops over the inventory space for a 1 x 1 size item, and at least 1 x 1 loops for a width x height size item. I’m sure there’s a smarter implementation that involves dynamic programming or something of the like, but this should be fast enough for a small inventory.

Oh should I make my own instead? and should I use gridlayout or use that inventory matrix?

Wait I think i understand that but slightly. I check if each tile has a size that can fit a part? Im really confused can you go more in depth?

Here’s the problem you’re looking at. It’s called the “bin packing problem.”

Unfortunately, this Wikipedia article is like many others and has not become very useful at solving this in a practical way.

Here’s one solution, which I think you should be able to implement. Here’s a link to the Python implementation. Transforming this to Lua (especially using a LLM) shouldn’t be too hard.

My recommendation instead is to treat this like a game design problem:

  1. Allow the user to sort their own inventory. This is why people use grid inventories anyway, to make a fun puzzle in managing the inventory.
  2. Upon pickup/addition just add it to the first available slot and then tell the user their inventory needs to be sorted.

Basically, I think you’re actually automating away part of the fun. If the point is to simply constrain the items the user can have based upon availability, I would not make a grid-layout a mechanic in the game, and instead rely upon some “Size” stat or something.

Best of luck.

2 Likes

You could use a UIListLayout/UIGridLayout. It has a special property called SortOrder. It’s set to LayoutOrder at default, so you can set the LayoutOrder per slot Frame. A slot with LayoutOrder 1 would be shown up first, etc.

Every time you add an item you’d have to recalculate all LayoutOrder values.

1 Like

image
So far when the player spawns in all their items arent sorted so they are in 1 spot. How would I detect if like there is space for one of the items? How would I detect if theres tiles that reserve space?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.