# How to manage grid-based inventory with items larger than 1x1 cell?

Me and my team have been trying to implement some sort of inventory system. We've set our eyes on an inventory system where some items may take up more than one box on the grid system, sort of like the one used in SOTS the pit, like this:

(source: webguyunlimited.com)

Notice how some items, such as the gun, take up more than one slot. How can we implement this? This includes checking to make sure that a slot we would like to insert an item in hasn't already been taken up (we may have irregular shaped items too).

• I'm not seeing what the irregular sizes has to do with the hot bar. The hot bar in your example appears to just hold a single item regardless of its size or shape. Is your question about the hot bar as your title suggests or about the inventory space like the question body suggest?
– House
Mar 21 '14 at 5:20

Store the position of the top left square of your item in var position:Array=[x, y]
Then add to each object an array of couples of ints representing the occupied coordinates of the grid, relative to the position of the top left square.
For example, a 1x1 object will have var size:Array = [[0,0]]
A 1x3 object (1 square wide and 4 squares tall) will have var size:Array = [[0,0], [0,1], [0,2]]
An object shaped like the T piece in Tetris will have var size:Array = [[0,0], [1,0], [1,1], [1,2], [2,1]]

Then if you need to check wether a certain position in the grid is free, say [8,2] (the position corresponding to the piece of meat in your picture) you can use a method like function checkFree (x:int, y:int):Boolean { for each (var item:Item in inventory){ for each (var square:Array in item.size){ if (item.position[0]+square[0]==x && item.position[1]+square[1]==y) retrurn false //position is occupied } } return true } 
Then, to see if an object will fit in a particular position:  checkFit (item:Item, x:int, y:int):Boolean{ for each (var square:Array in item.size){ if (!checkFree(x+square[0], y+square[0])) return false } retrun true } 
I used this in my tetris game, I think it will do for your inventory too :)

In my Torchlight clone demo, I just stored the inventory as a 2D array.

Each item specified inv_size_x and inv_size_y with a default of 1/1, but I could set for example:

item->setInvSize(1, 4); // example, a sword that is tall


Now I cheated a little since my bag also used sizing like this (for inserting to the bag, I used a brute force method to find the first space that would fit the item). I implemented the interface via events, so each "tile" in my inventory would receive an OnDragIn, OnDragOut, OnClick event. I would have an object Event passed through it, and a member Event.item // null if no item. From there I could check if my sword fit inside of an empty space by bruteforce-checking each sword tile with each inventory tile. I would do this on all 3 events, giving the user some visual feedback on my OnDragIn and OnDragOut and enforcing it on my OnClick.

Your implementation will vary, so I kept this as generic as possible.