Thread: a couple questions about inventory management system

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    26

    a couple questions about inventory management system

    So I've been messing with polymorphism and inheritance a lot recently. I have been reading everywhere and looking through code of some RPGs and realizing how beneficial it is for RPGs. I have been messing around with some stuff to try and find a way to implement it. I came up with something that I could possibly use for inventory and possibly other things.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    #define ITEM	00
    #define WEAPON	01
    #define POTION	02
    
    class Item
    {
    public:
    	Item(const string& name, const int& type): m_name(name), m_type(type) {};
    	virtual ~Item() {};
    	virtual void DoAction() = 0;
    protected:
    	string m_name;
    	int m_type;
    };
    
    class Weapon: public Item
    {
    public:
    	Weapon(const string& name, const int& type, const int& dmg): Item(name,type), m_dmg(dmg) {};
    	virtual ~Weapon() {};
    	virtual void DoAction();
    private:
    	int m_dmg;
    };
    
    void Weapon::DoAction()
    {
    	cout << "The weapon hits the enemy for " << m_dmg << " damage\n";
    }
    
    class Potion: public Item
    {
    public:
    	Potion(const string& name, const int& type, const int& amt): Item(name, type), m_healamt(amt) {};
    	virtual ~Potion() {};
    	virtual void DoAction();
    private:
    	int m_healamt;
    };
    
    void Potion::DoAction()
    {
    	cout << "The potions heals the user by " << m_healamt << " health\n";
    }
    
    int main()
    {
    	Item* sword = new Weapon("Sword", WEAPON, 34);
    	Item* healthPotion = new Potion("Health Potion", POTION, 26);
    	sword->DoAction();
    	healthPotion->DoAction();
    	cin.get();
    
    	delete sword;
    	delete healthPotion;
    
    	return 0;
    }
    Granted the weapon and potion are both split classes but i figured that once they were created there would be no reason to access the damage or healing amount. Basically my question is this. Is this a good way to work with inventory? is there a better way out there that wouldn't get ridiculously complex? I'm asking this because of the fact that they are split (in the event that I had to edit the values) and because virtual functions do use up a bit of memory to create. Any information would be appreciated.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    On the memory issue about the virtual functions: That is not really an important factor, the memory used will be minimal and unnoticeable.

    The overhead with virtual functions may be arguable, due to the fact of more layers of indirection. Although I have yet to see a substantial reduction in performance due to polymorphism.

    Designing the Inventory system really depends on your games specific needs. If you give a little input into what types of items, and the uses of special typed items then I would be able to better help you.

    I have one word of advice that I cannot stress enough. Do NOT try to create a Inventory manager that can handle anything. Try to create a manager that does exactly what you need it to do, nothing more or less. Also, try to create it in a way that is not dependent on any other module of your software. That way should you need to upgrade due to new features(or lack thereof ) then you don't have to redesign everything just because of this one module.

    Sorry if I am nagging or inconclusive, but if you need to know anything else ask with wording that is not so vague.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple of questions about XOR linked lists...
    By klawson88 in forum C Programming
    Replies: 5
    Last Post: 04-19-2009, 04:55 PM
  2. a couple of questions about System.String
    By Elkvis in forum C# Programming
    Replies: 5
    Last Post: 02-17-2009, 02:48 PM
  3. A couple of Basic questions
    By ozumsafa in forum C Programming
    Replies: 8
    Last Post: 09-26-2007, 04:06 PM
  4. Couple of Questions
    By toonlover in forum Windows Programming
    Replies: 10
    Last Post: 07-14-2006, 01:04 AM
  5. couple questions for newb
    By bluehead in forum C++ Programming
    Replies: 10
    Last Post: 11-24-2001, 03:32 AM