Question

So here's what I got in a nutshell.

  1. I have a base item class from which, potions, equipment, spells, etc.. are derived and several classes are derived from those and so on. Note: some derived classes have non-virtual class-specific member functions / data types.

  2. I have also created a "random" armor/weapon generator.

What I want to have is an inventory kind of like this:

struct Hero_Inventory
{
   std::vector<Spell*> Spell_Inventory;
   std::vector<Potion*> Potion_Inventory;
   std::vector<Equipment*> Equipment_Inventory; 

   Hero_Inventory() {}
};

Creating a container (inventory) for my spells and potions has been pretty straight forward. As they will be predefined. Using polymorphism and pointers wont be a problem

My main problem is figuring out to store and use (ex. access the weapon-class's specific member functions) my randomly generated weapons / armor (which are both derived from Equipment).

I would like for all of my "Equipment" to be in one container.

I'm generating all of my Equipment in a few functions so when I finish the generation process and end up with something like this:

Equipment * TestArmor = new Armor(/* Bunch of parameters go here */);

I don't know what to do with it, because as soon as that function goes out of scope I loose that pointer. And without pointers / referencing I can't use polymorphism, which is allowing me to keep my Equipment all in one container.

I'm really lost at this point and I'm looking for any suggestions or alternatives people can suggest. If you need more code I'll post it, just tell me what part(s) your interested in.

Sorry if I've been vague, this is my fist post. I'm usually pretty good at figuring things out on my own but this thing has me beat. If anyone needs more information ask and I'll try to provide it.

Thanks in advance, -Ryan

Was it helpful?

Solution

Create a uniform interface for manipulating the Equipment objects:

class Equipment {
public:
    virtual void render(Renderer& renderer) = 0;
    virtual void createController(ControllerManager& controllerManager) = 0;
    virtual void load(std::istream& input) = 0;
    virtual void save(std::ostream& output) = 0;
};

The function createController is interesting as the equipment will be able to tell the ControllerManager how to create something that will manipulate its exact values.

If you want a function which allows interaction with other Equipment objects, you probably need the Visitor Pattern. This is a way of introducing polymorphism based on multiple types rather than a single type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top