best design to items system

hello everybody!

i was moving forward on my little RPG, and came across the item system problem. up to now, i'm able to load an item list from an external file.

now, i want to make a system to handle all items in the game.
i tought in a single main vector for all items. and, then, inside a player class, a vector of pointers to the main vector of items.
but then i came across a problem: there are different kind of items, Weapon, Armor, and Others. all of these classes inherit some members of a base class ItemBase.

since everybody loves code, here is an example:
and I get the error:
class 'ItemBase' has no member named 'setAttack'.

this is true, as 'setAttack' is a member of 'ItemWeapon' class, which is inherited from BaseClass.

at this point, i did read this topic:
in which is said that "downcasting is a sign of bad design", so how to organize everything?

any suggestion is apreciated. i don't care if I have to change the whole system, if there are better solutions :)
Well, if you have a pointer of class Item, and you want it to be a new object of Weapon, it will only be allowed to use functions allowed for Item.

So, you can either create a function setAttack for item, just for inheritance purpose, or you can do some general-purpose virtual function, that will get always called for every item and each item will define it (so, for Weapon, it would have setAttack inside).

And if you want to differ items, you can make an enum in Item, which will hold class ID.
In each constructor, you would have to set class ID to corresponding ID.

Or, if these don't work for you, you can find your own way around.

As for "downcasting is bad design" - you are learning; First off, it it works and is clear, it isn't that bad. Second, sometimes it's better to do something against rules but in a way so that it works, then try to follow the rules at all costs, resulting in not completing your program.

thanks for your reply.
yes, i tried it with downcasting. but it got a bit confusing... i just wanted to know if there are other ways :)

so, if i do not use downcasting, it's impossible to create an array of objects from different classes (even if all of these inherit from a base class)? and why is that bad design, after all?
and does most of the alternatives rely on making different arrays (or vectors), one for each type of item?

thanks again!

ps: now it came to my mind, i think that the topic name should be "better design", and not "best design"...
Last edited on
polymorphism is the way. But the fact you want a single vector to hold ALL items in your game seems a bit wrong to me.

edit: you could also look at the 'command' design pattern:

which is kind of what you were doing. all you need to do is give baseClass an execute() method and make all your items implement that execute method in their different ways. only problem with that is the word execute() is very descriptive given a context.
Last edited on
it's ok, i'm not attached to the idea of having a single vector to hold everything.

but then, if i'll need a vector to each item type, how to keep track of items possessed by the player? maybe a vector that points to the other vectors? how could i do that?

thanks again! :)
Topic archived. No new replies allowed.