item and inventory system

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

item and inventory system

Post 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?
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: item and inventory system

Post 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.
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: item and inventory system

Post 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.
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: item and inventory system

Post 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.
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: item and inventory system

Post 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.
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: item and inventory system

Post 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: .
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: item and inventory system

Post by mv2112 »

I have another question, if i create a deconstructor for the object class, can it be inherited?
User avatar
Bullet Pulse
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 89
Joined: Sun Feb 21, 2010 6:25 pm

Re: item and inventory system

Post 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();
Image
Post Reply