Inventory System

Hey peeps.
I'm trying to create an inventory system in C++ using classes and objects.
Here is what I have now

Item.h
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
#pragma once

#include <iostream>
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>

class Item
{
private:
protected:
	enum itemType {NOTHING = 0, CONSUMABLE, CHEST, LEGS};

	ALLEGRO_BITMAP *image;
	std::string name;
	int type;

public:
	Item();
	~Item();
	Item(std::string filePath, std::string name, int type);

	void setName(std::string var);
	std::string getName();

	void setType(int var);
	int getType();
};


Item.cpp
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
#include "Item.h"


Item::Item()
{
	image = NULL;
	name = "";
	type = NOTHING;
}


Item::~Item()
{
	al_destroy_bitmap(image);
}

Item::Item(std::string filePath, std::string name, int type)
{
	image = al_load_bitmap(filePath.c_str());
	this->name = name;
	this->type = type;
}

void Item::setName(std::string var)
{
	name = var;
}
std::string Item::getName()
{
	return name;
}

void Item::setType(int var)
{
	type = var;
}
int Item::getType()
{
	return type;
}

void Consumable::use(int position)
{
}


Inventory.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#pragma once

#include "Item.h"

class Inventory
{
private:
	Item inventory[28];

public:

	Inventory();
	~Inventory();
};


Inventory.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "Inventory.h"


Inventory::Inventory()
{
}


Inventory::~Inventory()
{
}


Basically what I want my inventory system to become is this:
http://i.imgur.com/YHhk1Gj.png
Each slot can hold an item.

Basically this inventory system should be able to do what RS Inventory system can do. (Hold items, Use items, Equip Items (No need for moving item))
How would I go about this?
I've not reached a Point yet where I'm gonna implement a inventory System in my platformer (lol and ill probably never implement one because its a platformer and I've never seen a platformer with inventory). I'm gonna add power ups, which are quite similar to consumable items. If you want I'll send you my code, once i've added those...


So to my Suggestion:
I'd create a list. Something like:

std::vector<Item*> ItemList; //probably as a singleton

Thats where you save your Items. You could set the type of each item individually. Vectors are (usually) quite easy to handle. Whenever you want to add a new item, use ItemList->push_back(). You can just iterate through the vector and set the Destination rectangle for each Item in your inventory. You could also do it with an Array of Item-Objects, like you did (Why Limit the Array to 28 elements, if your example has only 10 squares? O_o).

EDIT: You should probably add some kind of Timer to your item class, to set the Duration of consumable items. To equip items, add a public integer variable to your item class (or an Access function or whatever you like).

int ItemState;

1
2
3
4
5
 enum {IS_NOT_ACTIVE = 0, //not consumable, not active
       IS_ACTIVE = 1, //not consumable, but active
       IS_CONSUMED = 2, //the Moment you press on a consumable item
       IN_USE = 3, //when countdown is running down
       ..........};



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//iterate through your vector, with iterator or for-loop
if (ItemList[n]->ItemState == IS_NOT_ACTIVE)
{
    continue;
}
else if (ItemList[n]->ItemState == IS_ACTIVE)
{
    //Add the stats of the item to your Player
}
else if (ItemList[n]->ItemState == IS_CONSUMED)
{
    ItemList[n]->SetCountdown(ItemList[n]->GetCountDownOfItem()); //Gets Duration of consumable item and Counts down
}
else if (ItemList[n]->Type == IS_CONSUMABLE && ItemList[n]-> ItemState == IN_USE)
{
    if (ItemList[n]->GetCountdown() <= 0)
    {
        ItemList->erase(n); //If countdown reach 0, remove it from the itemlist
    }
} //dunno if that code compiles correctly, but should give you an idea 


I didnt say anything on how to arrange them like in your example (2x5), but that should be quite obvious. Set the size of the Picture in your itemclass and multiply it with the number in the vector it has. as soon as two of them are printed, increase the Y-Position with the size of your Picture. (Hope this works for allegro, never used it)

You should probably create a prototype Project to test some things out... You maybe wanna use inheritance / Abstract base classes to stick with the oop Approach.

I haven't found anything useful with graphics (only with text rpgs) on the Internet, but I hope I could help you...
Last edited on
Thanks for your reply, was really helpful, for future ideas.
I get than I can use a vector as my resizable array, which is what I intended to do.
The picture has 10 squares because it was a quick example..

But the real problem is with inheritance like you said.
I want to create an item class, and then a consumable class that inherits the basics from Item class.
I don't know how to approach it in a way, that I can get it working.
I've tried it this way;
1
2
3
4
5
6
7
8
9
10
11
class Consumable : Public Item
{
private:
protected:


public:
	Consumable();
	~Consumable();
	void use();
};

But I just don't know how to approach it properly and get it working.. :/

Off-topic:
Also just an idea for your platformer.
There is actually the game, Maplestory, which has an inventory, and which is a platformer ;)
Sup :)

Never heard of Maplestory but it sure does sound like fun :D

Inheritance is actually quite easy once you got the hang of it. If you want I can upload an inheritance example for a simple menusystem to github... It would be using SDL2, but thats irrelevant for the inheritance part.

Basically, your item class would contain pure virtual methods. The classes inherting (this should be the verb of ineritance) your item, would use the exactly same funtions, but are not declared with the keyword virtual. So basically you are copying the funtions from your item file in your classes which inhert item. You can then define those funtction individually. So i would rather not create a class call consumable, but a class for each item. (Somehow it sounds retarded again, creating tons of classes) Well anyway, so you'd have a class called potion. And a class called OgreSword.

The inheritance way is just a suggestion from me, you can also do it with your item class alone, without 10 diffrent itemclasses ;)


As for the render part:
1
2
3
4
5
6
7
8
9
10
11
12
13
 
for (int i = 0; i < ItemList.size();i++)
{
    ItemList[i].PositionX = (i%2)*ImageWidth;

    if (i % 2 == 0) // after two items continue on next line
    {
        ItemList[i].PositionY += ImageHeight;
        ItemList[i].PositionX = 0; //restart on left side
    }

    ItemList[i].RenderAt(PositionX, PositionY, ImageWidth,ImageHeight);
}


Id love to help you developing some kind of inventory system but sadly i'm currently missin the time to work on other peoples projects.At least not in the next few weeks... Try out some opensource projects ho do have an inventory. Im pretty sure that there are sone quite experienced programmers here wholl be able to help you
An example of what (i think) halfnoob is explaining:

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
#include<vector>
#include<iostream>
#include<memory>

class Item
{
public:
	virtual void Use() = 0;


};


class Consumable : public Item
{
	void Use()
	{
		std::cout << "Drinking my consumable" << std::endl;
	}
};

class Sword : public Item
{
	void Use()
	{
		std::cout << "Putting the pointy end of my sword into the baddy" << std::endl;
	}
};

int main()
{
	// create an empty inventory
	std::vector<std::unique_ptr<Item>> inventory;

	// add some test data
	inventory.push_back(std::make_unique<Sword>());
	inventory.push_back(std::make_unique<Consumable>());


	// use stuff in the inventory. 
	// Notice how it's not class specific when we call Use(), 
	// as we are using a base class pointer
	for (int i = 0; i < inventory.size(); i++)
	{
		std::unique_ptr<Item> item(std::move(inventory.at(i)));

		item->Use();
	}

	return 0;
}


I don't think you should make any of this a singleton though.
Thats what i meant ^^ (I only want the List to be a singleton)
Topic archived. No new replies allowed.