How should i make a inventory system for my text adventure?

This is a discussion on How should i make a inventory system for my text adventure? within the Game Programming forums, part of the General Programming Boards category; Ive made like 60 text games all unfinished and this one is the second largest one ive ever made and ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    114

    How should i make a inventory system for my text adventure?

    Ive made like 60 text games all unfinished and this one is the second largest one ive ever made and im wondering how i should go about making an inventory system that allows me to add items to it view the items(text wise) and modify such as drop use if consumable.

    Ive made a Class for items
    Code:
    class Item
    {
          private: 
                   string Itemname;
                   int itemnumber;
                   int ItemBaseValue;
                   string ItemDescription;
                   int MaxItemCharges;
                   bool Weapon;
                   bool Consumable;
                   bool Helmet;
                   bool Gloves;
                   bool Body;
                   bool Boots;
                   bool Accessory;
                   int WeaponBase;
                   int HealBase;
          public:
                     void setItemnumber(int number)
                     {
                          itemnumber = number;
                     }
                     int GetItemnumber()
                     {
                          return(itemnumber);
                     }                 
                     string getItemname()
                     {
                            return(Itemname);
                     }
                     int getItemBaseValue()
                     {
                            return(ItemBaseValue);
                     } 
                     int getWeaponBase()
                     {
                            return(WeaponBase);
                     }
                     int getHealBase()
                     {
                            return(HealBase);
                     }
                     string getType()
                     {
                       string type;     
                       if(Weapon = true)
                       {
                          type = "Weapon";
                       }
                       if(Consumable == true)
                       {
                          type = "Consumable"; 
                       }        
                       if(Helmet == true)
                       {
                          type = "Helmet";
                       }   
                       if(Gloves == true)
                       {
                          type = "Gloves";
                       }
                       if(Body == true)
                       {
                          type = "Body";
                       } 
                       if(Boots == true)
                       {
                          type = "Boots";
                       }
                       if(Accessory == true)
                       {
                          type = "Accessory";
                       } 
                       return(type);
                     }                                                      
                     void setItemname(string name)
                     {
                       Itemname = name;
                     }
                     void setItemdesc( string description)
                     {
                          ItemDescription = description;
                     }
                     void setHealBase(int number)
                     {
                         HealBase = number; 
                     }
                     void setItemvalue(int number)
                     {
                        ItemBaseValue = number; 
                     }
                     void setWbase(int number)
                     {
                       WeaponBase = number;
                     }   
                     void setType(string type)
                     {
                       if(type == "Weapon")
                       {
                          Weapon = true;
                       }
                       if(type == "Consumable")
                       {
                          Consumable = true;
                       }        
                       if(type == "Helmet")
                       {
                          Helmet = true;
                       }   
                       if(type == "Gloves")
                       {
                          Gloves = true;
                       }
                       if(type == "Body")
                       {
                          Body = true;
                       } 
                       if(type == "Boots")
                       {
                          Boots = true;
                       }
                       if(type == "Accessory")
                       {
                          Accessory = true;
                       }     
                     }                                                                                      
                     int getitemtype(int number)
                     {
                         number = 0;            
                         if(Weapon == true)
                         {
                              number = 1;        
                         }
                         if(Body == true)
                         {
                              number = 2;                                         
                         }
                         if(Gloves == true)
                         {
                              number = 3;                                    
                         }
                         if(Boots == true)
                         {
                              number = 4;                                   
                         }
                         if(Accessory == true)
                         {
                              number = 5;                                    
                         } 
                         if(Helmet == true)
                         {
                              number = 6;                                    
                         }  
                         if(Consumable == true)
                         {
                              number = 7;                                    
                         }
                         return(number);                                                                                                         
                     }
    };
    But when it comes to making an inventory I'm not doing well. So far all i have is to make the inventory items easily accessible i made a inventoryslot array global and an inventoryslotamount array. One is like an inventory slot the other is the amount of the same item in that slot. But implimenting this seems to be a problem for me. Cause when i don't know what im doing i get bored of it. Any Ideas?
    Who needs a signature?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    if(Weapon = true)
    Is it one =, or maybe two ==
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Didn't see that, it might have caused a problem in the far future.
    Who needs a signature?

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Does anyone have a idea .
    Who needs a signature?

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,438
    The usual process for an inventory depends on item types being defined in a class hierarchy. So Item is usually an abstract class and everything else derives from it; Weapon derives from Item, Armor derives from Item, Helmet derives from Armor, etc.

    This way you can define the inventory as a simply vector of references to Item:
    vector<&Item> Inventory;

    Or can create containers just for armor types, for instance, by:
    vector<&Armor> Armory;

    Because all objects types belong to an hierarchy, the vector can receive the base class defined when it is declared and all derived classes from that class, as per the language rules concerning polymorphism. Then you wrap that vector inside a class and define the necessary functionality like searching the inventory, listing it, removing and adding items.

    ...

    If however you want to retain your current model, one way to avoid that mess of if statements you got yourself into, is to define an enum that lists all object types.

    Code:
    enum ItemType { 
        Weapon,
        Consumable,
        Helmet
        // etc...
    };
    
    class Item
    {
          private: 
                   string Itemname;
                   int itemnumber;
                   int ItemBaseValue;
                   string ItemDescription;
                   int MaxItemCharges;
                   ItemType type;
                   int WeaponBase;
                   int HealBase;
    
                   /* ... */
    };
    Last edited by Mario F.; 03-12-2010 at 05:53 AM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    You mean a better way than adding a bunch of new methods and variables every time you add an object?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Make an interface for item or at the very least a base class that has within it all the common behavior and functionality common to all items. Then derive each type of item from this base class.

    Any time you are identifying an object based on a query for it's type as in:

    Code:
    if (objecttype == somehardcodedtype)
    {
    }
    alarm bells should go off in your head that this is a bad approach.

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Haven't read to vectors yet but the enumerator type helped. Il read up on vectors and try then lol.
    Who needs a signature?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-13-2005, 09:03 AM
  2. Text adventure Idea...
    By none in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2002, 12:06 PM
  3. Simple Text Adventure: Need Help
    By Unregistered in forum Game Programming
    Replies: 19
    Last Post: 10-28-2001, 06:22 PM
  4. how to make 2 text window
    By bluexrogue in forum C Programming
    Replies: 2
    Last Post: 09-15-2001, 08:51 PM
  5. text adventure
    By BuRtAiNiAn FlY in forum C Programming
    Replies: 1
    Last Post: 09-05-2001, 09:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21