Sign in to follow this  
Peter Conn

Inventory

Recommended Posts

Hiya I have an inventory which is vector of "*Item"s. Then all the various item types are derived from this (Weapon, Shield and so on), how would I be able to access a Weapon only variable from the vector?

Share this post


Link to post
Share on other sites
First of all, what language are you using?

In C# you would do something like this:


foreach (Item item in inventory)
{
if (item is Weapon)
{
// Do stuff
}
}




Although i think the equivalent of vectors in C# are ArrayLists/Lists.

Share this post


Link to post
Share on other sites
If I understand ya properly, you should take a look into iterators (for vector scanning) and then add a function to your item class to return it's type for checking if it's a weapon.

Share this post


Link to post
Share on other sites
just a minor additional point

If you wanted to use it as a weapon after determining it is one you will need to type cast it

in c#:

foreach (Item item in inventory)
{
if (item is Weapon)
{
(item as Weapon).whatever();
// Do stuff
}
}



*edit* corrected source tags

Share this post


Link to post
Share on other sites
I assume you're using C++, but I don't know of anything native to the language. Perhaps like Aternus said you could have an id in the base item and change that value for each derived type so you know what you're looking for.

I did something similar to this once. I had an id variable in the base class and in the constructor of the derived class altered the value depending on the type it was. It seemed to work out alright. All of this is assuming i'm understanding what you want to do exactly [smile].

Share this post


Link to post
Share on other sites
You could use a dynamic_cast.

Something like this:


Item* yourObject; //this is a pointer to the object to test which class it belongs to
Weapon * weapon = dynamic_cast<Weapon *>(yourObject);
Shield * shield = dynamic_cast<Shield *>(yourObject);
if(weapon)
{
// object is of class Weapon
}

Share this post


Link to post
Share on other sites
assuming your using C++, there are two easy solutions to this:

one, as mentioned earlier, is just to add a tag (I suggest an enumeration) to your base class to identify each item:


enum ItemType{WEAPON, SHIELD, POTION};

class Item:
{
private:
ItemType type;
...
public:
ItemType getType()
{
return type;
}
...
}

...

std::vector<*Item> items;

for (std::vector<*Item>::iterator it = items.begin(); it != items.end(); ++it)
{
switch((*it)->getType())
{
case WEAPON:
weapon* pWeapon = reinterpret_cast<weapon*>(*it);
//do weapon stuff
break;

...

}
}




the other solution, which may or may not fit what your trying to do, would be to make a virtual use() function in your base class, and have each inherited class provide their own version of it. that way, you could go through your vector and just use() each item, without need for knowing what type each item is and without using casts.

edit: after re-readiong your original post, i don't think the virtual function way of doing things is what your looking for.

Share this post


Link to post
Share on other sites
You may also consider keeping the separately-typed things in separate containers instead.


struct Inventory {
std::vector<Weapon> weapons;
std::vector<Armour> armours;
// etc.
};


There is no "best solution" here universally; it depends on the totality of your usage patterns. Various different ways of organizing things will make certain tasks easier or cleaner at the expense of others.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this