28
Entity Component System Lunch & Learn 2015-01-21

Entity Component System

Embed Size (px)

Citation preview

Page 1: Entity Component System

Entity Component SystemLunch & Learn 2015-01-21

Page 2: Entity Component System

What is ECS?

Page 3: Entity Component System

ECS is a pattern that makes a distinction between entities, components and systems.

Page 4: Entity Component System

An entity is a discrete object representing an actor in a simulated space. Realize first that we are simulating a

universe - the game’s universe. All the visible, tangible ‘things’ in the universe can be represented as entities.

Entity

Page 5: Entity Component System
Page 6: Entity Component System

Can you think of some more examples?

Page 7: Entity Component System
Page 8: Entity Component System
Page 9: Entity Component System

A component describes a singular behavior ascribed to an entity. The name of a component should ideally satisfy the

sentence: “When applied to an entity, that entity now exhibits ____ behavior.”

Component

Page 10: Entity Component System

Component Behavior

Transform Transforms the entities position, rotation and scale (from the origin).

Health Attributes a health value, which can be increased or decreased by receiving ‘heal’ or ‘damage’ messages. At zero, it fires the message ‘died’.

Collider Entity can collide with others. The implementation will depend on the physics system, but this could be an abstract base component.

DamageOnCollision Will send a ‘damage’ message with a data-defined value to any entities it collides with.

ParticleEmitter The entity will emit particles, the nature of which depends on data-defined values.

Page 11: Entity Component System

Systems typically perform low level, non-gameplay functions such as rendering graphics, performing physics calculations or finding paths. Components should use systems to offload

computationally difficult workloads, as long as they are behaviorally related.

System

Page 12: Entity Component System

Systems can be totally unrelated to gameplay

in which case we usually call them ‘Services’.

Page 13: Entity Component System

Renderer

Finder

Parser

Builder

Loader

Calculator

Publisher

Manager

Page 14: Entity Component System

Why are we using ECS?

Page 15: Entity Component System

Inheritance

Page 16: Entity Component System

Composition

Page 17: Entity Component System

Classical Inheritance Entity Component

Pros

● Sub-classing can feel intuitive

● Lots of existing research

● Shorter, less complex code● Promotes thinking in terms

of behaviors● Very flexible● Emergent behavior● Enables scripting of

behavior by non-techies

Cons

● Leads to highly complex code

● Less flexible

● Difficult to apply correctly, easy to mis-use

● Good components require more thinking about design

Page 18: Entity Component System

DisclaimerECS is not the be-all end-all solution to game

programming.

Page 19: Entity Component System

How should we use ECS?

Page 20: Entity Component System

Components● Should be generic and re-usable.

● Configure via parameters from data, or from a system.

● Should describe a singular behavior.

● Avoid depending on other components too much.

● More than 200 LOC and you’re doing it wrong! Less than 100 LOC is very possible.

Page 21: Entity Component System

Think in terms of behaviorWhat do I want my entity to do? What is that

behavior called? Can I simplify it?

Page 22: Entity Component System

Generic versus Specific(components exercise)

Page 23: Entity Component System

Decouple componentsusing messages

Page 24: Entity Component System

Messages● Should be generic and re-usable.

● Pass useful information as parameters.

● Whenever you need to react to an event, first look if a suitable message is being sent.

● Don’t use messages if a component has a hard dependency that only be solved by one specific kind of component.

Page 25: Entity Component System

Generic versus Specific(messages exercise)

Page 26: Entity Component System

Always remember the SRPSingle Responsibility Principle

Page 27: Entity Component System

Thanks for listening!

Page 28: Entity Component System

Questions?Ask me anything.