entity component system

Post on 15-Apr-2017

302 Views

Category:

Software

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Entity Component SystemLunch & Learn 2015-01-21

What is ECS?

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

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

Can you think of some more examples?

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

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.

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

Systems can be totally unrelated to gameplay

in which case we usually call them ‘Services’.

Renderer

Finder

Parser

Builder

Loader

Calculator

Publisher

Manager

Why are we using ECS?

Inheritance

Composition

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

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

programming.

How should we use ECS?

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.

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

behavior called? Can I simplify it?

Generic versus Specific(components exercise)

Decouple componentsusing messages

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.

Generic versus Specific(messages exercise)

Always remember the SRPSingle Responsibility Principle

Thanks for listening!

Questions?Ask me anything.

top related