Video Game Inventory System

I'm making a game for the console, and I have run into a bit of a problem: a lot of the game revolves on buying and selling items, and I can't work out a good way to have an inventory system. My general goals are to have each of the following:
-Everything defined in a class
-Cost, name, weight, etc.
-The ability to add an item to the inventory by referring to an item with an ID(like 002 or 935 or something)

This is what's I got:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class inventory
{
public:
	struct Items
	{
		string name;
		int price;
		int lvlreq;
		int id;
		int slot;
	}item[20];
	void AddItem(string Name, int Price, int Lvlreq, int Id)
	{
		item[islot].name = Name;
		item[islot].price = Price;
		item[islot].lvlreq = Lvlreq;
		islot++;
	}
}inventory;

//ITEM STRUCTS//
//For item types, 1 = consumable/usable, 2 = passive bonus, 3 = quest item, 4 = vendor trash, 5 = useless

struct item
{
	string name;
	int price;
	int lvlreq;
	int id;
};


It almost works, but I still don't really like it. Any suggestions?
closed account (3hM2Nwbp)
A quick type-up, I haven't ran it though a compiler. This is how I'd do it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
template<typename Item_Type, unsigned int capacity>
class Container
{
	public:

		Container(void)
		{

		}

		// Returns true if the item was added successfully, false if the container was full
		bool add(const Item_Type& item)
		{
			if(capacity > items.size())
			{
				items.push_back(item);
				//item.onInsertionFailed() /* A trick I've done in the past to give custom behavior */
				return true;
			}
			return false;
		}

		unsigned int getCapacity(void) const
		{
			return capacity;
		}

		unsigned int getSize(void) const
		{
			return items.size();
		}

		// May throw
		const Item_Type& get(unsigned int index) const
		{
			return items.at(index);
		}

		// Returns true if the index was removed, false if the index was out of bounds
		bool remove(unsigned int index)
		{
			if(index < items.size())
			{
				items.erase(items.begin() + index);
				return true;
			}
			return false;
		}
		
		~Container(void)
		{

		}

	private:

		std::vector<Item_Type> items;
};

class MyItem
{
};

int main()
{
  Container<MyItem, 30> inventory; // Make a container that can hold 30 MyItems
  inventory.add(MyItem());
  const MyItem& item = inventory.get(0);
  inventory.remove(0);
  return 0;
}
Last edited on
Thanks so much, that was really helpful!
Topic archived. No new replies allowed.