The computing scientists main challenge is not to get confused by - - PowerPoint PPT Presentation

the computing scientist s main challenge is not to get
SMART_READER_LITE
LIVE PREVIEW

The computing scientists main challenge is not to get confused by - - PowerPoint PPT Presentation

The computing scientists main challenge is not to get confused by the complexities of their own making. E. W. Dijkstra Entity-Component Systems + You ( They're not just for games anymore ) @parisba | @The_McJones | @TheMartianLife


slide-1
SLIDE 1

“The computing scientist’s main challenge is not to get confused by the complexities of their

  • wn making. ”
  • E. W. Dijkstra
slide-2
SLIDE 2

Entity-Component

Systems + You

(They're not just for games anymore)

@parisba | @The_McJones | @TheMartianLife 2
slide-3
SLIDE 3

Hello!

» Paris (@parisba) » Game developer, PhD Computing, Author » Tim (@The_McJones) » Game developer, PhD Computing, Author » Mars (@TheMartianLife) » Software developer, Research & Teaching Assistant, Upcoming Author @parisba | @The_McJones | @TheMartianLife 3
slide-4
SLIDE 4
slide-5
SLIDE 5
slide-6
SLIDE 6

ECS?

@parisba | @The_McJones | @TheMartianLife 6
slide-7
SLIDE 7

If you Google ECS, you'll probably get Unity, or Amazon.

Or something entirely unrelated.

Sorry. @parisba | @The_McJones | @TheMartianLife 7
slide-8
SLIDE 8

This is not a games talk!

Mostly. @parisba | @The_McJones | @TheMartianLife 8
slide-9
SLIDE 9

Before we start..

Is anyone using ECS, or working in games? @parisba | @The_McJones | @TheMartianLife 9
slide-10
SLIDE 10

Overview

» What is ECS? » History Lesson » ECS Implementations » What ECS is good for » What ECS is not good for » Summary @parisba | @The_McJones | @TheMartianLife 10
slide-11
SLIDE 11

What is ECS?

@parisba | @The_McJones | @TheMartianLife 11
slide-12
SLIDE 12

Entity Component Systems

@parisba | @The_McJones | @TheMartianLife 12
slide-13
SLIDE 13 There's the short answer, and the long answer... @parisba | @The_McJones | @TheMartianLife 13
slide-14
SLIDE 14

ECS?!

@parisba | @The_McJones | @TheMartianLife 14
slide-15
SLIDE 15

Strict separation between data and logic

@parisba | @The_McJones | @TheMartianLife 15
slide-16
SLIDE 16

Oh, ECS...

@parisba | @The_McJones | @TheMartianLife 16
slide-17
SLIDE 17

The Impetus

Games really only have two needs: » performance » flexibility @parisba | @The_McJones | @TheMartianLife 17
slide-18
SLIDE 18

Performance

(you've only got ~16.6 milliseconds)

@parisba | @The_McJones | @TheMartianLife 18
slide-19
SLIDE 19

Flexibility

(with jillions of objects and factors)

@parisba | @The_McJones | @TheMartianLife 19
slide-20
SLIDE 20

ECS encourages both

Performance + Flexibility

@parisba | @The_McJones | @TheMartianLife 20
slide-21
SLIDE 21

The key word being 'encourages'

You can, of course, mess it up. @parisba | @The_McJones | @TheMartianLife 21
slide-22
SLIDE 22

So what?

Everything is fine already! @parisba | @The_McJones | @TheMartianLife 22
slide-23
SLIDE 23
slide-24
SLIDE 24
slide-25
SLIDE 25
slide-26
SLIDE 26

It's a bit messy.

@parisba | @The_McJones | @TheMartianLife 26
slide-27
SLIDE 27

God Objects

@parisba | @The_McJones | @TheMartianLife 27
slide-28
SLIDE 28
slide-29
SLIDE 29

History Lesson!

@parisba | @The_McJones | @TheMartianLife 29
slide-30
SLIDE 30

Early 'ECS'

@parisba | @The_McJones | @TheMartianLife 30
slide-31
SLIDE 31
slide-32
SLIDE 32

Adam Martin's ECS

» Entities have IDs » Components have data » Systems have logic @parisba | @The_McJones | @TheMartianLife 32
slide-33
SLIDE 33

Let's look at these

  • ne by one...
@parisba | @The_McJones | @TheMartianLife 33
slide-34
SLIDE 34

Entity

» an object in your game, uh, software » anything, really, a player, an enemy, anything » an identifier, no logic @parisba | @The_McJones | @TheMartianLife 34
slide-35
SLIDE 35

Entity

Two excellent descriptions of an entity (in games): » everything but the terrain » assemblages @parisba | @The_McJones | @TheMartianLife 35
slide-36
SLIDE 36

Entities are boring.

Just a thing to reference. @parisba | @The_McJones | @TheMartianLife 36
slide-37
SLIDE 37

Isn't this just a primary key?

@parisba | @The_McJones | @TheMartianLife 37
slide-38
SLIDE 38

Yes, yes it is.

@parisba | @The_McJones | @TheMartianLife 38
slide-39
SLIDE 39

Components

» it has some data @parisba | @The_McJones | @TheMartianLife 39
slide-40
SLIDE 40

Components

Also pretty boring.

A flag that may also hold raw data for one aspect of a thing. @parisba | @The_McJones | @TheMartianLife 40
slide-41
SLIDE 41

System

@parisba | @The_McJones | @TheMartianLife 41
slide-42
SLIDE 42

Systems are the interesting bit.

Logic that runs continuously and performs actions on every Entity that has a Component of the aspect the System is designed to work with. @parisba | @The_McJones | @TheMartianLife 42
slide-43
SLIDE 43

ECS or EC?

@parisba | @The_McJones | @TheMartianLife 43
slide-44
SLIDE 44

ECS in use today

@parisba | @The_McJones | @TheMartianLife 44
slide-45
SLIDE 45

It's very popular.

In games.

@parisba | @The_McJones | @TheMartianLife 45
slide-46
SLIDE 46

To do this...

@parisba | @The_McJones | @TheMartianLife 46
slide-47
SLIDE 47

We're gonna talk about games. Sorry.

@parisba | @The_McJones | @TheMartianLife 47
slide-48
SLIDE 48

Games are very dynamic

@parisba | @The_McJones | @TheMartianLife 48
slide-49
SLIDE 49

Game worlds are built out of entities

@parisba | @The_McJones | @TheMartianLife 49
slide-50
SLIDE 50

A tree is different from a human who is different from an alien who is different from...

@parisba | @The_McJones | @TheMartianLife 50
slide-51
SLIDE 51

Games are in constant development flux

@parisba | @The_McJones | @TheMartianLife 51
slide-52
SLIDE 52 @parisba | @The_McJones | @TheMartianLife 52
slide-53
SLIDE 53 @parisba | @The_McJones | @TheMartianLife 53
slide-54
SLIDE 54

games are made up of moving parts

@parisba | @The_McJones | @TheMartianLife 54
slide-55
SLIDE 55

But...

@parisba | @The_McJones | @TheMartianLife 55
slide-56
SLIDE 56

There is common pool of aspects

» Position data » Visuals data » Animation data » Sound data » Gameplay data @parisba | @The_McJones | @TheMartianLife 56
slide-57
SLIDE 57

What a game seems like

1.Player presses walk forward 2.Move the player forward 3.Check if enemy can see the player avatar 4.Move towards the player 5.If in range swing sword 6.Deal damage to the player @parisba | @The_McJones | @TheMartianLife 57
slide-58
SLIDE 58

What a game is actually doing

1.Receive user input 2.Raycast from enemy to player avatar 3.Update avatar and enemy position 4.Deal damage 5.Update animations 6.Render frame @parisba | @The_McJones | @TheMartianLife 58
slide-59
SLIDE 59

Separating data from behaviour

@parisba | @The_McJones | @TheMartianLife 59
slide-60
SLIDE 60

Objects?

@parisba | @The_McJones | @TheMartianLife 60
slide-61
SLIDE 61

Objects and (rigid, mostly) class hierarchies

@parisba | @The_McJones | @TheMartianLife 61
slide-62
SLIDE 62

Complex hierarchies

@parisba | @The_McJones | @TheMartianLife 62
slide-63
SLIDE 63
slide-64
SLIDE 64

Composition, not Inheritance

@parisba | @The_McJones | @TheMartianLife 64
slide-65
SLIDE 65

Composing entities

@parisba | @The_McJones | @TheMartianLife 65
slide-66
SLIDE 66

Composing entities

Benefits:
  • 1. Easy to add new entities.
  • 2. Easy to change entities.
  • 3. Performant!
@parisba | @The_McJones | @TheMartianLife 66
slide-67
SLIDE 67
slide-68
SLIDE 68

ECS and Games, now

@parisba | @The_McJones | @TheMartianLife 68
slide-69
SLIDE 69

Entity

@parisba | @The_McJones | @TheMartianLife 69
slide-70
SLIDE 70

Entity

Examples:
  • Crate
  • Ball
  • Enemy
  • Player
  • Traffic light
@parisba | @The_McJones | @TheMartianLife 70
slide-71
SLIDE 71

Component

@parisba | @The_McJones | @TheMartianLife 71
slide-72
SLIDE 72

Component Examples

» position (an x and a y) » velocity » sprite(s) » health value » character name » player (tag) @parisba | @The_McJones | @TheMartianLife 72
slide-73
SLIDE 73

System

@parisba | @The_McJones | @TheMartianLife 73
slide-74
SLIDE 74

System Examples

» player control » render » gravity » movement » AI control @parisba | @The_McJones | @TheMartianLife 74
slide-75
SLIDE 75

Games, Games, Games

@parisba | @The_McJones | @TheMartianLife 75
slide-76
SLIDE 76

ECS doesn't necessarily mean less code.

Theoretically it means better code, though. @parisba | @The_McJones | @TheMartianLife 76
slide-77
SLIDE 77

What ECS is good for

@parisba | @The_McJones | @TheMartianLife 77
slide-78
SLIDE 78

Performance?

@parisba | @The_McJones | @TheMartianLife 78
slide-79
SLIDE 79

“The purpose of all programs, and all parts of those programs, is to transform data from

  • ne form to another.”
Mike Acton
slide-80
SLIDE 80

Data Oriented Design

@parisba | @The_McJones | @TheMartianLife 80
slide-81
SLIDE 81

Data-oriented Design is tricky

@parisba | @The_McJones | @TheMartianLife 81
slide-82
SLIDE 82

You never do just

  • ne thing
@parisba | @The_McJones | @TheMartianLife 82
slide-83
SLIDE 83
slide-84
SLIDE 84
slide-85
SLIDE 85
slide-86
SLIDE 86

Cache Misses

@parisba | @The_McJones | @TheMartianLife 86
slide-87
SLIDE 87

Cache misses

suck

@parisba | @The_McJones | @TheMartianLife 87
slide-88
SLIDE 88
slide-89
SLIDE 89

Data-oriented Design = Programming for Good Memory Access

@parisba | @The_McJones | @TheMartianLife 89
slide-90
SLIDE 90

ECS encourages Data-

  • riented Design
@parisba | @The_McJones | @TheMartianLife 90
slide-91
SLIDE 91

Caches, memory speed, oh my!

@parisba | @The_McJones | @TheMartianLife 91
slide-92
SLIDE 92

Parallelisation

@parisba | @The_McJones | @TheMartianLife 92
slide-93
SLIDE 93

Lots of tasks. Large set of data.

@parisba | @The_McJones | @TheMartianLife 93
slide-94
SLIDE 94

Structure around consuming data as a stream.

@parisba | @The_McJones | @TheMartianLife 94
slide-95
SLIDE 95

Complex, interlocking systems.

@parisba | @The_McJones | @TheMartianLife 95
slide-96
SLIDE 96

Composability

The biggest advantage

@parisba | @The_McJones | @TheMartianLife 96
slide-97
SLIDE 97

ECS is great for...

» GUI Programming » Editors » Audio systems » VFX systems » Simulations » A service registry » ... almost anything that's event-driven » ... anything you want to easily unit test @parisba | @The_McJones | @TheMartianLife 97
slide-98
SLIDE 98

GUI Programming

@parisba | @The_McJones | @TheMartianLife 98
slide-99
SLIDE 99

A NEAT SIDE-EFFECT OF ECS!

@parisba | @The_McJones | @TheMartianLife 99
slide-100
SLIDE 100

In practice, it's not that easy...

@parisba | @The_McJones | @TheMartianLife 100
slide-101
SLIDE 101

What ECS is not good for

» Disk heavy operations » Unchanging data and procedures » Heavily hierarchical data @parisba | @The_McJones | @TheMartianLife 101
slide-102
SLIDE 102

How would you go about

making an ECS?

@parisba | @The_McJones | @TheMartianLife 102
slide-103
SLIDE 103

Quick and Dirty way

@parisba | @The_McJones | @TheMartianLife 103
slide-104
SLIDE 104

Start with our entities

@parisba | @The_McJones | @TheMartianLife 104
slide-105
SLIDE 105 entities int[] = [0,1,2,3,4,5...] /* player skeleton with sword skeleton with shield camera etc etc */ @parisba | @The_McJones | @TheMartianLife 105
slide-106
SLIDE 106

now we need some components

@parisba | @The_McJones | @TheMartianLife 106
slide-107
SLIDE 107 TransformComponent { position rotation scale } MovementComponent { speed direction } HealthComponent { life } ShieldComponent { reduction } DamageComponent { damage target } RenderComponent { mesh textures } PlayerComponent {} CameraComponent {} @parisba | @The_McJones | @TheMartianLife 107
slide-108
SLIDE 108

how do we connect entities and components?

@parisba | @The_McJones | @TheMartianLife 108
slide-109
SLIDE 109 Game { // all our entities... entities int[] = [0,1,2,3,4,5...] // all of these are of size entities long damages DamageComponent[] transforms TransformComponent[] renders RenderComponent[] movers MovementComponent[] healths HealthComponent[] shields ShieldComponent[] players PlayerComponent[] cameras CameraComponent[] @parisba | @The_McJones | @TheMartianLife 109
slide-110
SLIDE 110 damages = [nil, 1, 1, 3, 3, 1, nil] healths = [100, 5, 5, 5, 5, 5, nil] @parisba | @The_McJones | @TheMartianLife 110
slide-111
SLIDE 111

What about the systems?

@parisba | @The_McJones | @TheMartianLife 111
slide-112
SLIDE 112 updateFrame(deltaTime) { // first we get user input and use that to update player position input = getInput() updatePlayerSpeed(input, players) // then move everything that can be moved // including the player moveEntities(movers, transforms, deltaTime) // then we deal damage damageEntities(damages, healths) // then we reduce damage for everyone with shields reduceDamage(shields, healths) // then we render the frame renderFrame(renders, transforms, cameras) } @parisba | @The_McJones | @TheMartianLife 112
slide-113
SLIDE 113 moveEntities(movers, transforms, deltaTime) { for mover, transform in movers, transforms { transform.position += mover.velocity * deltaTime } } @parisba | @The_McJones | @TheMartianLife 113
slide-114
SLIDE 114
slide-115
SLIDE 115
slide-116
SLIDE 116

It gets better, of course

This approach isn't the best for large scale, but its the simplest way to get started. @parisba | @The_McJones | @TheMartianLife 116
slide-117
SLIDE 117

Existing

implementations

@parisba | @The_McJones | @TheMartianLife 117
slide-118
SLIDE 118

ECS implementations

» Rust (Specs): https://github.com/slide-rs/specs » C++ (EnTT): https://github.com/skypjack/entt » Elixir: https://github.com/yosriady/ecs » C# (Entitas): https://github.com/sschmid/Entitas-CSharp » Java (Artemis): http://gamadu.com/artemis/ » C# (EgoCS): https://github.com/andoowhy/EgoCS » C++ (EntityX): https://github.com/alecthomas/entityx » C++ (Anax): https://github.com/miguelmartin75/anax » ...and many, many more! @parisba | @The_McJones | @TheMartianLife 118
slide-119
SLIDE 119

None of this is new.

@parisba | @The_McJones | @TheMartianLife 119
slide-120
SLIDE 120

Summary

@parisba | @The_McJones | @TheMartianLife 120
slide-121
SLIDE 121

Entities Components Systems

@parisba | @The_McJones | @TheMartianLife 121
slide-122
SLIDE 122

ECS = Composable

@parisba | @The_McJones | @TheMartianLife 122
slide-123
SLIDE 123

ECS is composition

  • n steroids
@parisba | @The_McJones | @TheMartianLife 123
slide-124
SLIDE 124

Data-oriented Design

@parisba | @The_McJones | @TheMartianLife 124
slide-125
SLIDE 125

Strengths

» Performance (hopefully) » Flexibility » No heirarchy to remember » Systems have many of the advantages of microservices/functional programming @parisba | @The_McJones | @TheMartianLife 125
slide-126
SLIDE 126

Weaknesses

» More code upfront » Harder to keep everything in your head » No clear starting point @parisba | @The_McJones | @TheMartianLife 126
slide-127
SLIDE 127

Pick and choose the ideas you like

There is no one 'right way'. @parisba | @The_McJones | @TheMartianLife 127
slide-128
SLIDE 128

Resources

» Slides will go up on the SACon website shortly » Talk resources can be found at https://lab.to/SAConNYC19Resources @parisba | @The_McJones | @TheMartianLife 128
slide-129
SLIDE 129

Review us!

@parisba | @The_McJones | @TheMartianLife 129
slide-130
SLIDE 130

Thank you!

» Find us online: » @parisba » @The_McJones » @TheMartianLife » Chat with us at 'Meet the Experts'! 3:05PM today! Sponsor Pavilion! Come and tell us we're wrong! » Stay warm + enjoy the rest of your #OReillySACon <3 @parisba | @The_McJones | @TheMartianLife 130