Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm in the midst of building a text-based game with hope I can implement an inventory system among other things to make the world interactive.

I have already created an item class:
C++
enum ItemRange {
    ShortRange,
    MidRange,
    LongRange,
};

enum ItemType { 
    Weapon,
    Shield,
    Consumable,
    Necklance,
    Ring,
    Robe,
    Shirt,
    Pants,
    Helmet,
    Chestplate,
    Pauldron,
    Gauntlet,
    Greaves,
    Boots,
};

class Item
{
    private: 
            std::string Name;
            std::string material;
            int dataBaseID;
            std::string itemDesc;
            int Attack;
            double CritMulti;
            int Defence;
            int HealVal;
            int restoreMana;
            int restoreStamina;
            int GoldValue;
            int maxItemCharges;
            int enchantVal;
            ItemType type;
            ItemRange range;
            int stack;
            int maxStack;
            bool stackable;

    public:
        std::string displayName;
        bool equipped;
        Item();
        ~Item();
        Item(std::string Name, std::string material,std::string ItemDesc, int DatabaseID, ItemType Type, int Attack, double CritMulti, int Defence, int HealVal, int restoreMana, int restoreStamina, int GoldValue, int Stack, int MaxStack, bool Stackable, int enchantVal, ItemRange range);
        friend void isEquipped(std::string funcChoice, std::unordered_map<std::string, Item> funcMap);
        std::string GetItem() {return Name;}
        std::string GetItemDesc() {return itemDesc;}
        std::string GetDispName() {return displayName;}
        int GetDatabaseID() {return dataBaseID;}
        ItemType GetItemType() {return type;}
        ItemRange GetItemRange() {return range;}
        int GetAttack() {return Attack;}
        int GetCritMulti() {return CritMulti;}
        int GetDefence() {return Defence;}
        int GetHealVal() {return HealVal;}
        int GetRestoreManaVal() {return restoreMana;}
        int GetRestoreStaminaVal() {return restoreStamina;}
        int GetGoldVal() {return GoldValue;}
        int GetStack() {return stack;}
        int GetMaxStack() {return maxStack;}
        bool GetStackable() {return stackable;}
        bool GetEquipped() {return equipped;}

};

Item::Item() {
    Name = " ";
    material = " ";
    dataBaseID = 0;
    itemDesc = " ";
    maxItemCharges = 0;
    type = Weapon;
    Attack = 0;
    CritMulti = 0.0;
    HealVal = 0;
    Defence = 0;
    restoreMana = 0;
    restoreStamina = 0;
    GoldValue = 0;
    enchantVal = 0;
    stack = 0;
    maxStack = 0;
    stackable = false;
};

Item::~Item() {

};

Item::Item(std::string Name, std::string material,std::string ItemDesc, int DatabaseID, ItemType Type, int Attack, double CritMulti, int Defence, int HealVal, int restoreMana, int restoreStamina, int GoldValue, int Stack, int MaxStack, bool Stackable, int enchantVal, ItemRange range) {
    this->Name = Name;
    this->material = material;
    this->displayName = material + " " + Name;
    this->dataBaseID = DatabaseID;
    this->itemDesc = ItemDesc;
    this->maxItemCharges = enchantVal * 2 / 4;
    this->type = Type;
    this->enchantVal = enchantVal;
    this->stack = Stack;
    this->maxStack = MaxStack;
    this->stackable = Stackable;
}


While I'm happy with the Item class I'm not sure the best approach to interacting with them while in the inventory.

My recent attempt at doing so:

C++
std::string equippedStringChoice;
bool funcLoop_1 = true;
int funcChoice_1 = 0;
Item itemToCheck;

void isEquipped(std::string funcChoice, std::unordered_map<std::string, Item> funcMap) {
    if (funcMap.find(funcChoice) == funcMap.end()) {
        std::cout << "This item is not in your inventory!\n";
    } else {  
        itemToCheck = funcMap.at(funcChoice);
        switch(itemToCheck.type) {
            case 0:
                while(funcLoop_1) {
                    funcLoop_1 = false;
                    std::cout << "Which hand do you want to equip it too?\n1: Main-Hand?\n2: Off-Hand?\n";
                    std::cin >> funcChoice_1;
                    switch(funcChoice_1) {
                        case 1:
                            if (player.weaponEquipped != "empty") {
                                std::cout << "You already have" << player.weaponEquipped << "equipped in your main hand!\n";
                            } else {
                                std::cout << "You equipped " << itemToCheck.GetDispName() << "\n";
                                player.weaponEquipped = itemToCheck.GetDispName();
                                player.damageMin += itemToCheck.Attack;
                                player.damageMax += itemToCheck.Attack;
                            }
                            break;
                        case 2:
                            if (player.offhandWeaponEquipped != "empty") {
                                std::cout << "You already have " << player.offhandWeaponEquipped << " equipped in your main hand!\n";
                            } else {
                                player.offhandWeaponEquipped = itemToCheck.GetDispName();
                                player.damageMax += itemToCheck.Attack;
                                player.damageMin += itemToCheck.Attack;
                            }
                            break;
                    }
                }
                break;
            case 1:
                while(funcLoop_1) {
                    funcLoop_1 = false;
                    std::cout << "Do you want to equip " << itemToCheck.GetDispName() << "\n1: Yes\n2: No\n> ";
                    std::cin >> funcChoice_1;
                    switch(funcChoice_1) {
                        case 1:
                            if (player.offhandWeaponEquipped != "empty") {
                                std::cout << "You already have " << player.offhandWeaponEquipped << " equipped in your off hand!\n";
                            } else {
                                player.offhandWeaponEquipped = itemToCheck.GetDispName();
                                player.damageMin += itemToCheck.Attack;
                                player.damageMax += itemToCheck.Attack;
                                player.armorClass += itemToCheck.Defence;
                            }
                            break;
                        case 2:
                            std::cout << "Ok\n";
                            break;
                        default:
                            funcLoop_1 = true;
                    }
                }
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                break;
            case 5:
                break;
            case 6:
                break;
            case 7:
                break;
            case 8:
                break;
            case 9:
                break;
            case 10:
                break;
            case 11:
                break;
            case 12:
                break;
            case 13:
                break;
        }
    }
}


For context, I'm using an unordered_map as the inventory.

Any help is appreciated! If it wasn't already clear I'm asking what the most effective way to build this inventory system is.

What I have tried:

Experimented with Vectors and Lists and Google.
Posted
Updated 21-Jan-24 12:36pm
Comments
Member 15627495 22-Jan-24 1:25am    
It's a first attempt.

to light your code, move all the "text Strings" in an Array outside the Item Class.

your class 'Item' is so messy, but you will see better by working on it.

Do you use UML design to prepare your Classes and Objects ?

just to tell you there are too much in your class.

PLAYER --> use --> ITEMS
you code it like "ITEMS --> compounds --> PLAYER".

study few "RPG Class Modeling", you will go further.
Starts are always hard stuff.
Brennon Nevels 22-Jan-24 6:25am    
Hmm, that makes sense. That's probably why my head get so twisted sometimes understanding my own code.
[no name] 23-Jan-24 12:35pm    
The question of "is equiped?" is something AI asks (itself); not a user ... who "knows" what he has (in his inventory ... implying a "display"). As you're building, there are always at least 2 player: the user and (the game's) AI. And certain "questions" just don't make sense given the context.
Brennon Nevels 24-Jan-24 7:59am    
From how I see it, the computer needs to display the inventory, then take input to interact with an item?
[no name] 24-Jan-24 11:37am    
Yes ... Give the user all the information he's entitled too. "Check bag". Show it's contents. If he "selects" something, it means he wants to use it; or discard it if his bag is full. If it's not availiable for a given scene, disable it (menu) instead of interrupting things by "messaging" that he can't use it. If you don't like playing your game, no one else will.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900