Inheritance VS Components

Not being familiar with components before starting to use Unity3D i am very used to inheritance and polymorphism.

Im starting to get a feel for using scripts as components to reuse functionality. This seems to be able to solve reusability needs that i am used to solving with inheritance.

Id like to hear your experiences. Do you use inheritance, composition or both and in which case is one technique favorable?

:roll_eyes:

Both. (Or all three, since composition isn’t exactly component-based design)

They are not mutually exclusive. Now… The following is from what I understand. Since I never went to any fancy coding university, I may be wrong…

Components becomes handy when you can break down a specific behaviour into smaller parts that are interchangeable and can be mixed with each other.

Composition is useful when those smaller part have a very well defined role to play, and therefor can be derived from a specific base.

Inheritance comes into play when using composition, or when different components shares similar logic or members.

Just to be clear on the difference between components and composition… Let’s assume;

public class GameObject
{
    public List<Component> components;
}

This would be component-base design. You don’t know what component you get, and what they do exactly. You get a collection of… something.

As for composition;

public class GameObject
{
    public Camera;
    public Light;
    public ITransform;
}

In composition, you know you’ll get something deriving from a specific base class, most often from abstract or interface declaration. You know exactly what you get, even if you don’t know how the derived class will implement it.

In both case, you build something from smaller part. In one, you have no idea of the part, while in the other you enforce a specific definition of what those parts are.

However, in all cases, inheritance should be an important part of your design. Any pieces can derive from another or implement an interface for communication.

Example;

// Every character can take damage, they have HP, Inventory
public abstract Character : MonoBehaviour, IDamageable 

// Player is a character, controlled by input
public sealed Player : Character
{
//Define the set of base attribute. Is the player a knight? a mage? a hunter?
//Can be done by inheritance, or by composition. 
//In this case, the composition has the advantage of mixing abilities. Could the player be 2 classes? 3?
    public PlayerClass class; 
}

// NonPlayingCharacter have path finding logic
public abstract NonPlayingCharacter : Character 
{
// Composition, the pathfinding may differ from one game to another, or outside vs in a dongeon
    public PathFinding; 
}

// QuestGiver can be interacted with by the player.
public QuestGiver : NonPlayingCharacter, IInteractive 
{
// "Component", list of quest this character can give, may even change the behaviour of this character.
    public List<Quest> quests; 
}

// Enemy target Players and try to attack it, can also be damaged by the player
public abstract Enemy : NonPlayingCharacter 
{
// Collection of item the enemy can drop when killed
    public List<Item> items; 
}

//Specific enemy behaviour, stay afare and shot the player with rockets.
public RocketLauncher : Enemy 
{
//On the data version, we could have rocket launcher of different color dealing more of less damage, rate of fire
    public Weapon rocket; 
//The ammo can have different behaviour. Shot 3 rocket at once? Fragmentation? Bounce?
    public Ammo ammo; 
}
1 Like