# How do you structure an Item class?

I've been thinking about the structure of an Item class. There are many items which may or may not have effects in the game.

For example, an item "Herb" has an effect to "Heal HP by 5". but an item "Stone" has no effect.

As per a title, when you design a game, how do you structure the Item class?

The following is the structure I've come up with.

class Item{
String id;
// Status where s = HP, MP, ATK .. etc
// Integer -> value which affects on the Status
Map<Status, Integer> effect;
SignType st; // +, -, *, /
}

class Player{
Map<Status, Integer> status; // Status where s = HP, MP, ATK .. etc
}

• if your items are somewhat very varied in stuff they could contain and how they can be treated, maybe you should use an entity component system. – v.oddou Oct 14 '15 at 7:30
• – Anko Oct 14 '15 at 8:37
• Clarification request: Are all item effects active (like "heal HP"), or are some passive (like "+10 max HP while carried")? What is the problem with your current structure? – Anko Oct 14 '15 at 8:38
• All items effects are active at this point but, might be expanded in the future like you mentioned. – neko Oct 15 '15 at 0:22
• well the problem is current structure cannot handle items which have no effect such as "Stone" described at the post. – neko Oct 15 '15 at 0:29

Have one base class Item. Then create two sub class, one for Items with effect and another for Items without effect, like this-

class Item {
String id;
}

class EItem extends Item {
Map<Status, Integer> effect;
SignType st;
}

class WEItem extends Item {
}

• This looks good. but when you check the item, you would need to use "instance of" right? and I heard it is not recommended to use it for some reasons(I forgot). It's simple yet good. – neko Oct 15 '15 at 0:57
• This question has almost the same requirement as yours. Have a look at the answers - stackoverflow.com/questions/8841577/… – kazisami Oct 15 '15 at 14:48

If you want to keep things simple and each item independent, your current solution looks good. However, you only need addition and multiplication, the other two can be built with inverse operands.

In the long term, the question is if a set of predefined operations is flexible enough. What if a developer wants more sophisticated conditions, e.g.

If the item owner carries a sword, triple his attack damage.

In that case, you would need access to other items, possibly through a query interface. However, as soon as you use dynamic code, you can't easily serialize it anymore.

Remember that object oriented programming is usually using some form of data structure. It is probably easier to think of it in that format.

So... when creating an item. A common implementation is to just make an "onUse" function. Which... is ok, but kinda pointless. The item may have more than one effect. So... lets make it more interesting.

Class Item : {
private:
array<Action> ActionList;
public:
Use(Target, Target2 = NULL){
for( However many actions in action list)
ActionList.onUse(Target, Target2)
};

}


Here, our item has a target and an action list. The target is who the item is being used on. Of course you can always pass an array for multiple targets.

So...now the action list. What is an action list? If you are familiar with components, then it is basically the same thing. Action Lists are modules of code with data that is created on instantiation via factory or something.

First the interphase.

Class Action {
virtual update(Target, Target2 = Null) =0;
}


Next the implementation.

Class RestoreHP : Action {
private:
_healthRestore;
public:
RestoreHP(HealthRestore);
Override onUse(Target, Target2 = Null);
}
private:
_shieldStrength;
_shieldDuration;
public: