• Advertisement
Sign in to follow this  

Creating an Inventory System with Objects

This topic is 2689 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been working on making a text based rpg. I'm currently taking an intro to programming course in college and have been teaching my self c++ faster than the instructor. I've just learned about objects and have been trying to use them in my game (I've got a Player, Enemy, and Forest class right now).

Currently my inventory consists of a list of variables (sword, shield, dust...) and I'm trying to figure out how to make a real inventory system, but I'm not sure how it would work. I was thinking I would make a base item class, and from that derive equipable, consumable and craftable item classes, and derive from equipable class the weapon class and armor class. Then have an inventory class and from that make a equipped object and backpack object. But I'm not sure how to get all the parts to work together. I thought about putting an object for every item in the inventory class and each item object would have a variable to for the amount of that item in the inventory, but that doesn't seem practical. Could I get some advise on how to construct this?

Share this post


Link to post
Share on other sites
Advertisement
enum EquipSlot {
SLOT_BAG,
SLOT_HEAD,
SLOT_HAND
};

struct Equipable {
EquipSlot slot;
bool isEquipped;
};

struct Consumable {
float hpIncrease;
float hpIncreaseTime;
float consumeTimer;
};

struct Item {
//Player *owner; etc
Equipable *equip;
Consumeable *consume;
};

struct InventorySlot {
EquipSlot slot;
Item *item;
};

struct Inventory {
InventorySlot slots[8] = { SLOT_HEAD, SLOT_HAND };

bool Equip(Item &);
};

bool Inventory::Equip(Item &item) {
if (item.equip != null && !item.isEquipped) {
for (InventorySlot *i = slots; i < slots + 8; ++i) {
if (!i->item && i->slot == item.slot) {
i->item = item;
item.isEquipped = true;
return true;
}
}
}
return false;
}



Then a few factory functions to create items of the appropriate type.

Share this post


Link to post
Share on other sites
I had read about using vectors and pointers while googling. I read through the section on vectors in my textbook and they seem fairly simple, but pointers I've read quite a bit about and they still confuse me. Also I wouldn't be able to stor weapon, armor, and usable usable objects in one vector, though now that I think about it I could have a vector for each class. I guess I'll start experimenting with them.

Share this post


Link to post
Share on other sites
Quote:
Also I wouldn't be able to stor weapon, armor, and usable usable objects in one vector, though now that I think about it I could have a vector for each class.
There's a variety of ways you could store all of your item types in a single container. If you're fairly new to C++, the most straightforward way would probably be to derive each class directly or indirectly from a common base class (which you appear to be doing already), and storing pointers to instances of those classes in the container. (Note however that this isn't the only solution possible.)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement