Page 1 of 1

item and inventory system

Posted: Sun Mar 28, 2010 8:08 pm
by mv2112
Im trying to create an inventory system that indexes all the objects a player has. All of the objects inherit properties from an object base class so it's easier to index in the inventory system. My problem is that i cant figure out how i can access the member functions of an item if the inventory stores it in a vector of object pointers (base objects) instead of the item itself. I could, in the base class, create a bunch of pure virtual functions for each object but i want to know if there is an easier way to do this.

Code: Select all

class object
{
protected:
int x;
public:
virtual int RetX()=0;
};

class thingy:public object
{
public:
thingy();
int RetY();
int RetX();
private:
int y;
}

class inventory
{
private:
vector<object*> inv; //I cant access RetY from here
};
Is there another way to index objects efficiently?

Re: item and inventory system

Posted: Sun Mar 28, 2010 8:11 pm
by XianForce

Code: Select all

class object
{
protected:
int x;
public:
virtual int RetX()=0;
[b]virtual int RetY()=0;[/b]
};
Could that perhaps be why? If you can't access RetY, but you can access RetX, then that should solve it.

Re: item and inventory system

Posted: Sun Mar 28, 2010 8:19 pm
by mv2112
XianForce wrote:

Code: Select all

class object
{
protected:
int x;
public:
virtual int RetX()=0;
[b]virtual int RetY()=0;[/b]
};
Could that perhaps be why? If you can't access RetY, but you can access RetX, then that should solve it.
But if i had 100 different objects with multiple different functions and variables(and different variables types with the same name), i would have a base class with over a hundred different virtual functions which would be a pain to write.

Re: item and inventory system

Posted: Mon Mar 29, 2010 11:49 am
by XianForce
mv2112 wrote:
XianForce wrote:

Code: Select all

class object
{
protected:
int x;
public:
virtual int RetX()=0;
[b]virtual int RetY()=0;[/b]
};
Could that perhaps be why? If you can't access RetY, but you can access RetX, then that should solve it.
But if i had 100 different objects with multiple different functions and variables(and different variables types with the same name), i would have a base class with over a hundred different virtual functions which would be a pain to write.
Well why would they all have different functions?

I mean all objects will probably have mostly the same functions, they will just do something a little different.

Like for example, you could have a base object class like this:

Code: Select all

class Object
{
private:
     Rectangle mRect;
     char m_cType;
     std::string mDescription;
public:
     Object();
     ~Object();

     virtual std::string Examine(); //Returns a string of text to output when this object is examined
     virtual bool Use(); //Uses the object; Returns false if the object cannot be used
And from there, whatever other functions you need you make >.>

So an example object could be something like:

Code: Select all

class Potion : public Object
{
private:
     //You'd probably have whatever your API's interpretation of an image is here, for rendering purposes
public:
     Potion() { m_cType = TYPE_HEALING; mDescription = "A solution that restores 20 health points";}
     ~Potion(); //Free any resources the potion has here...

     bool Use() {player.health += 20}
So, that's the gist of it... Obviously there would be some big questions regarding design when doing this, because the objects will probably need access to things such as the player... But that's another question, for another day I suppose.

I hope that helps...

Oh and if you want to use specific functions from the derived class, with a pointer to the base class, you can just cast it.

Re: item and inventory system

Posted: Mon Mar 29, 2010 2:04 pm
by RyanPridgeon
If you're only holding pointers of type the base class, and pointing them to subclasses of your base class, it doesn't matter if you add loads more functions. You just need some common functions which your entity manager uses, such as Update, to be present in all subclasses.

Re: item and inventory system

Posted: Mon Mar 29, 2010 3:06 pm
by mv2112
XianForce wrote:
mv2112 wrote:
XianForce wrote:

Code: Select all

class object
{
protected:
int x;
public:
virtual int RetX()=0;
[b]virtual int RetY()=0;[/b]
};
Could that perhaps be why? If you can't access RetY, but you can access RetX, then that should solve it.
But if i had 100 different objects with multiple different functions and variables(and different variables types with the same name), i would have a base class with over a hundred different virtual functions which would be a pain to write.
Well why would they all have different functions?

I mean all objects will probably have mostly the same functions, they will just do something a little different.

Like for example, you could have a base object class like this:

Code: Select all

class Object
{
private:
     Rectangle mRect;
     char m_cType;
     std::string mDescription;
public:
     Object();
     ~Object();

     virtual std::string Examine(); //Returns a string of text to output when this object is examined
     virtual bool Use(); //Uses the object; Returns false if the object cannot be used
And from there, whatever other functions you need you make >.>

So an example object could be something like:

Code: Select all

class Potion : public Object
{
private:
     //You'd probably have whatever your API's interpretation of an image is here, for rendering purposes
public:
     Potion() { m_cType = TYPE_HEALING; mDescription = "A solution that restores 20 health points";}
     ~Potion(); //Free any resources the potion has here...

     bool Use() {player.health += 20}
So, that's the gist of it... Obviously there would be some big questions regarding design when doing this, because the objects will probably need access to things such as the player... But that's another question, for another day I suppose.

I hope that helps...

Oh and if you want to use specific functions from the derived class, with a pointer to the base class, you can just cast it.
You know, when you actually think about it, the way you suggested does sound better :worship: .

Re: item and inventory system

Posted: Mon Mar 29, 2010 4:17 pm
by mv2112
I have another question, if i create a deconstructor for the object class, can it be inherited?

Re: item and inventory system

Posted: Mon Mar 29, 2010 7:34 pm
by Bullet Pulse
mv2112 wrote:I have another question, if i create a deconstructor for the object class, can it be inherited?
Definitely.

When you delete a pointer to a base class that points to an object of a derived class, only the base class' destructor is going to be called for that object.

However, if you create a virtual destructor in the base class, when you delete that pointer, the derived class' destructor will be called, which then calls on base class' destructor.

Code: Select all

virtual ~Object();