Sign in to follow this  
orangedragon10

(C++) Simple Inventory System

Recommended Posts

I'm interested in setting up a simple class-based inventory system for multiple items and would like some ideas. I have my inventory class and item class. Each item has a name, description, and value. I want the inventory system to keep track of how many items I have of each unique type. So if I have 2 apples, and I want to add another apple, it simply adds it to my total amount of apples instead of replacing my current set of apples.

I was thinking of using a the STL map using <string, int> but I want the container to actually store the objects themselves so I can still access GetDescription(), GetValue(), etc straight from container and still keep track of the quantity of each item type.

Share this post


Link to post
Share on other sites
This is a pretty broad issue with many many different possible implementations.

I don't think you want to use a map, consider something like Vector<InventorySlot> and maybe a simple linear search for identical object types on adding an item would work just fine. Each vector index could be the slot id.

InventorySlot might be as simple as:

struct InventorySlot {
Item *ItemHandle;
int ItemCount;
}

You may want to encapsulate this if you plan on exposing InventorySlots to users outside of your inventory system.

It's hard to be too specific without actually implementing such a system for you, or without understanding more of your requirements or how you handle items.

Share this post


Link to post
Share on other sites
Do all the items of a given type have the same name, description, and value?

If so, you could use a map whose keys are strings (the name of the item type), and whose values are a user-defined type that stores the description, value, and count for that item type.

If not, you could use a map whose keys are the names of the item types, and whose values are a container (e.g. vector or list) of a user-defined type that stores the name, description, and value.

Those are just a couple of options, of course; there are many other ways you could set it up as well.

Share this post


Link to post
Share on other sites
M2tM: thank you for that option. I'll give it a whirl! never thought of having an inventory system -> item slot struct -> item object.

jvk: yes, each particular type of item (apples) has same stats. I was told it was a bad idea to store the quantity inside of the item itself, so I'm trying to avoid that.

Share this post


Link to post
Share on other sites
Imagine it as a database problem. There's a table containing info on items (name, color, size) and a second table with a pairing of (name, count).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this