Scripting Inventory system on grid map

Thanks for taking the time to read this post. Currently i’m trying to setup a inventory system on a grid based map (32x32). What i’d like to have is that each tile/grid space can hold more than one item. Here are the 3 ways i was thinking of doing this:

  1. have a 3D array array(width, height, items reference) but this would make the number fixed

  2. convert the x,y grid array to just simply a list of 1024 rooms and use a hashtable, where the key is a roomnumber and the value is an arraylist of items (but this might require quite the re-write of existing code)

  3. create a hashtable with the key as an x,y array, and the value as an arraylist of items. This would be ideal, but does JS in Unity support this?

What would the amazing unity community (that rhymed) recommend?

I’d have made a dictionary (hash) using the Grid Cell # as the index and the value being a class object that has an Items list as one of it’s members.

In J/U script that would be something like this.

import System.Collections.Generic;

var Grid : Dictionary.<int,GridCell>;
function Start()
	{
	Grid = new Dictionary.<int,GridCell>();
	}

//base class for a grid cell.
class GridCell
	{
	var items : List.<int>; //these int items represent the Id of an item type
	function GridCell()
		{
		items = new List.<int>();
		}
	}

Ya that’s kind of what I was thinking for option 2. Thank you for the example, I may look in to that as an option. The more I think about it I may even just go with option 1 as it’s clean and simple, and i’m OCD and don’t want people messing up the grid with 30 items on one tile. I think I might make the area have a small fixed amount.

Just create a 2D array of List.

Your base 32x32 map will always be there ( I am assuming ) so the data structure which makes sense is a 2D array. (conceptually and technically). Items will be picked up/ taken from grid spaces which is perfect for a list.

But that’s too easy! And by that I mean thank you. That’s pretty much exactly what I was looking for. I had no idea that arrays could contain lists