logoalt Hacker News

flohofwoelast Wednesday at 8:03 AM1 replyview on HN

> For games? All the time.

Hmm, but game objects is exactly the popular use case where traditional inheritance breaks down first and composition makes much more sense? That's why the whole Entity-Component idea came up decades ago (like in Unity, long before the more modern column-database-like ECS designs).


Replies

llmslave2last Wednesday at 8:28 AM

Entity systems still rely on inheritance as you typically have an Entity baseclass that all entities derive from. That's just a single level of inheritance. Unity does this through MonoBehavior. Unreal is more inheritance-heavy but developers typically subclass something like `Actor` to one level of additional inheritance beyond the Engine's subclasses. A lot of engines will have multiple superclasses behind a Entity class, but that's an implementation detail of the engine itself and to the game developer, it's treated as a single base class that is typically subclassed a single time for the entity itself.

Even in ECS's you will often find inheritance. Some implementations have you inherit a Component struct, others will have the systems inherit a System class.

I'm sure it's still used today in some engines and by some developers but the overwhelming opinion is that doing something like Entity -> Actor -> Monster -> Orc -> IceOrc is a bad idea. Instead it would be like

   class IceOrc : Entity { components {Health, Enemy, PlayerSeek, Position, etc} }
Where each component is like

   class Health : Component { value = 100 }
And yeah, they favour composition re. Components, it's just that the components tend to inherit from a Component class. But I would still call it composition!
show 1 reply