#A nudge in the right direction C# Unity

1 messages · Page 1 of 1 (latest)

lament tendon
#

Hello,
I'm hoping to make a inventory system of sorts,
I have a class called "Item" that has strings for name and description and a constructor.
If I wanted to make a Tool I could make a class "Tool" that inherits from item
and if I wanted a Weapon I could do the same
what if I wanted a Tool that is also a Weapon?

I've found some stuff on interfaces but they don't make as much sense to me, and I just want to make sure I'm on the right track before going down another rabbit hole lol.

lavish hare
#

As the name suggests, interfaces provide a way to interface with a given type, by forcing that type to publicly implement all members that the interface declares.
Meaning, if you had an interface IWeaponItem with a property int Damage { get; }, any type implementing IWeaponItem would need an int property called Damage, that has a public getter.

Interfaces may be a way to go for you, but depending on what they declare, you may end up with a lot of duplicate code across different item classes.

An alternative to that would be composition.

lament tendon
#

@lavish hare thanks for the info, I hadn't heard of the term Composition before so im glad to add that to my libraby