#### Archived

This topic is now archived and is closed to further replies.

# Text RPG - Player Inventory - Inheritance?

## Recommended Posts

Hello, I''m working on a text RPG and need to set up the player''s inventory structure. This is what I want. 1) A backpack vector, a vector for what the person is wearing, and variables for what is in the hands. 2) The backpack vector can hold any type of item. Containers (which hold any type of item), weapons, armor, potions, etc. If it is too difficult, I won''t do containers. I have some code. How could I properly implement this? How can I make a vector that will allow any type of item to be in it? Here''s some crap code I have now. The inheritance code I just stuck in there, I was using it to try making items before.
  // items.h // handles the player''s inventory #ifndef ITEMS_H #define ITEMS_H #include #include #include #include const int LEN = 80; // maximum length of item names class item // item class { private: char shortname[LEN]; // short item name char name[LEN]; // long item name char look[LEN]; // long item description unsigned long value; // item value (0+) double weight; // item weight (>0) public: void getData() { cin.getline(name, LEN, ''\n''); cout << "Enter full item name (ex. a notched scimitar): "; cin.getline(name, LEN, ''\n''); cout << "Enter short item name (ex. scimitar): "; cin.getline(shortname, LEN, ''\n''); cout << "Enter long item description: "; cin.getline(look, LEN, ''\n''); cout << "Enter item appraisal value (0+): "; cin >> value; cout << "Enter item weight (0+): "; cin >> weight; } }; class weapon : public item // weapon class { private: int minDamage; // minimum hit damage int maxDamage; // maximum hit damage public: void getData() { item::getData(); cout << "Enter minimum damage: "; cin >> minDamage; cout << "Enter maximum damage: "; cin >> maxDamage; } }; class armor : public item { private: int defense; // the maximum defense int bodyPart; // where the armor is worn public: void getData() { item::getData(); cout << "Enter defense: "; cin >> defense; bodyPart = getPosition(); } int getPosition() { cout << endl << "1 Head" << endl << "2 Chest" << endl << "3 Arms" << endl << "4 Hands" << endl << "5 Legs" << endl; cout << "Enter body position (1-5): "; cin >> bodyPart; return bodyPart; } }; class clothing : public item { private: int bodyPart; // where the clothing is worn public: void getData() { item::getData(); bodyPart = getPosition(); } int getPosition() { cout <> bodyPart; return bodyPart; } }; /*class container : public item { private: apvectoritems; int maxItems; // how many items it can hold double maxWeight; // how much weight it can hold int bodyPart; // where it is worn public: void getData() { item::getData(); cout << "How many items can it hold? "; cin >> maxItems; cout << "How much weight can it hold? "; cin >> maxWeight; bodyPart = getPosition(); } int getPosition() { cout <> bodyPart; return bodyPart; } }; */ class potion : public item { private: int plusHP; // how much health it will restore int plusMP; // how much magic power it will restore int maxUses; // how many times it can be used public: void getData() { item::getData(); cout << "How much health will it restore? "; cin >> plusHP; cout << "How much magic power will it restore? "; cin >> plusMP; cout << "How many usages per piece? "; cin >> maxUses; } }; class jewelry : public item { private: int bodyPart; public: void getData() { item::getData(); bodyPart = getPosition(); } int getPosition() { cout <> bodyPart; return bodyPart; } }; /////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////// class inventory { public: apvectorbackpack; apvectorbody; void dispBodyInv(apvector&body); void dispBackpackInv(apvector&backpack); void dispItemInv(item); private: }; void inventory::dispBodyInv(apvector&body) { body.resize(1); body[0].getData(); } void inventory::dispBackpackInv(apvector&backpack) { } #endif 

##### Share on other sites
Personally I would just make it a pointer to an item. Then it doesn''t have to handle allocation of the space, but the down side is that you do. Give the item a virtual class that returns the items type. You can make things a bit simplier by having a function that returns a boolean value indicating if a given action is valid for the item. Have each derived class call the same function in it''s parent so you can benefit from inheritance. When an action is performed if the item doesn''t know how to perform that action pass it to the parent class. If it makes it to the root then return invalid action and don''t let the user do what they attempted. A virtual function will take you to the top of the tree, but you can work your way back down manually.

##### Share on other sites
Holy classes! It´s been a long time since I´m out the web. Well, you could maybe make a linked list. But if you can store any kind of item... Are all them the same class, I mean, is a sword the same kind of object a key is? If so, it´s all right for a linked list.

##### Share on other sites
C/C++ Journal has an article on creating varients with member templates. That will get you around having to manage the space for the items. You still have to figure out what it is when you pull it out though. You could use run-time type identification to do that. If you implement their varient then you will be using RTTI anyway since they use dynamic_cast to throw an exception if you try to assign the varient to an invalid type.

##### Share on other sites
C/C++ Journal has an article on creating varients with function templates. That will get you around having to manage the space for the items. You still have to figure out what it is when you pull it out though. You could use run-time type identification to do that. If you implement their varient then you will be using RTTI anyway since they use dynamic_cast to throw an exception if you try to assign the varient to an invalid type.

• ### Forum Statistics

• Total Topics
628379
• Total Posts
2982353

• 10
• 9
• 15
• 24
• 11