game programming 02 - component-based entity systems
DESCRIPTION
Chapter 2 of the lecture Game Programming taught at HAW Hamburg. Introduction to and analysis of aggregation-based game models.TRANSCRIPT
![Page 1: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/1.jpg)
Game ProgrammingComponent-Based Entity Systems
Nick Prühs
![Page 2: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/2.jpg)
Objectives
• To understand the disadvantages of inheritance-based game
models
• To learn how to build an aggregation-based game model
• To understand the advantages and disadvantages of aggregation-
based game models
2 / 57
![Page 3: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/3.jpg)
Say you’re an engineer…
… set out to create a new Game Object System from scratch, and you’re going to ‘do it right the first
time’. You talk to your designer and say ‘What kind of content are we going to have in this game?’
They respond with ‘Oh lots of stuff, trees, and birds, and bushes, and keys and locks and … <trailing
off>’
And your eyes glaze over as you start thinking of fancy C++ ways to solve the problem.
The object oriented programming sages tell you to try to determine Is-A relationships and abstract
functionality and all that other fun stuff. You go to the book store and buy a C++ book just to be sure,
and it tells you to fire up your $5000 UML editor. [...]”
- Scott Bilas
3 / 57
![Page 4: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/4.jpg)
Entities
4 / 57
![Page 5: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/5.jpg)
Entities
5 / 57
![Page 6: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/6.jpg)
Entities
6 / 57
![Page 7: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/7.jpg)
Entities
7 / 57
![Page 8: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/8.jpg)
Entities
8 / 57
![Page 9: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/9.jpg)
Entities
9 / 57
![Page 10: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/10.jpg)
Entities
• object in your game world
• can (or cannot)…
be visible
move around
attack
explode
be targeted
become selected
follow a path
• common across all genres
10 / 57
![Page 11: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/11.jpg)
Entities
11 / 57
![Page 12: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/12.jpg)
Approach #1: Inheritance
12 / 57
![Page 13: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/13.jpg)
Approach #1: Inheritance
• Entity base class
• that class and its subclasses encapsulate the main game logic
13 / 57
![Page 14: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/14.jpg)
Example #1: Unreal Engine 3
• base class Actor
rendering
animation
sound
physics
• almost everything in Unreal is an Actor
Pawn extends by taking damage
Projectile extends by spawning impact effects
14 / 57
![Page 15: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/15.jpg)
Drawbacks of inheritance-based game models
• Diamond of Death
15 / 57
![Page 16: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/16.jpg)
Drawbacks of inheritance-based game models
• code added to the root of the inheritance tree causes big overhead
• code added to the leafs of the tree tends to get copied
• root and leaf classes tend to get very big
16 / 57
![Page 17: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/17.jpg)
Where is Waldo?
public override void TakeDamage(int damage)
{
this.Health -= damage;
}
17 / 57
![Page 18: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/18.jpg)
Where is Waldo?
public override void TakeDamage(int damage)
{
this.Health -= damage;
}
18 / 57
![Page 19: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/19.jpg)
Where is Waldo?
public override void TakeDamage(int damage)
{
base.TakeDamage(damage);
this.Health -= damage;
}
19 / 57
![Page 20: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/20.jpg)
Drawbacks of inheritance-based game models
• always need to understand all base classes along the inheritance tree
• impossible to enforce calling base class functions
Someone will forget it. Trust me.
o And you’re gonna spend your whole evening finding that one missing base.Update().
• deep class hierarchies will more likely run into call order issues
20 / 57
![Page 21: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/21.jpg)
Inheritance-based game models are…
• … difficult to develop
• … difficult to maintain
• … difficult to extend
21 / 57
![Page 22: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/22.jpg)
“There are probably hundreds of ways…
… you could decompose your systems and come up with a set of classes […], andeventually, all of them are wrong. This isn’t to say that they won’t work, but gamesare constantly changing, constantly invalidating your carefully planned designs. [...]
So you hand off your new Game Object System and go work on other things.
Then one day your designer says that they want a new type of “alien” asteroid thatacts just like a heat seeking missile, except it’s still an asteroid.”
- Scott Bilas
22 / 57
![Page 23: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/23.jpg)
“alien” asteroid
23 / 57
![Page 24: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/24.jpg)
Approach #2: Aggregation
24 / 57
![Page 25: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/25.jpg)
Approach #2: Aggregation
25 / 57
![Page 26: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/26.jpg)
Approach #2: Aggregation
• popular since Gas Powered Games’ Dungeon Siege
• introduced long before
• entities are aggregations of components
which in turn encapsulate independent functionality
• corresponds to recommendations by the Gang of Four
“favor object composition over class inheritance”
• similar approach is used by the Unity3D game engine
just for clarification: Unreal uses components as well, called ActorComponent
26 / 57
![Page 27: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/27.jpg)
Approach #2a
• create an Entity class
• add references to all available components
• has obvious disadvantages:
many component references will be null pointers for most entities
big unnecessary memory overhead
Entity class has to be updated each time a new component is
introduced
27 / 57
![Page 28: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/28.jpg)
Approach #2b
• create an Entity class
• introduce a common base class for components
• entities hold a collection of Component objects
reduced the memory overhead
increased extensibility
• already gets close to an optimal solution
easy to build, maintain and debug
easy to implement new design ideas without breaking existing
code
28 / 57
![Page 29: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/29.jpg)
However, we can do better.
29 / 57
![Page 30: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/30.jpg)
Approach #2c: Entity Systems
30 / 57
![Page 31: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/31.jpg)
Approach #2c: Entity Systems
31 / 57
![Page 32: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/32.jpg)
Approach #2c: Entity Systems
• game entities are nothing more than just an id
• thus, no data or methods on entities
• no methods on components, either: all functionality goes into what is
called a system
PhysicsSystem
HealthSystem
FightSystem
• entirely operate on their corresponding components
32 / 57
![Page 33: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/33.jpg)
“All the data goes into the Components.
All of it. Think you can take some “really common” data, e. g. the x-/y-
/z-coordinates of the in-game object, and put it into the Entity itself?
Nope. Don’t go there. As soon as you start migrating data into the
Entity, you’ve lost. By definition the only valid place for the data is
inside the Component.”
- Adam Martin
33 / 57
![Page 34: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/34.jpg)
Example #2: Simple Fight
34 / 57
![Page 35: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/35.jpg)
Example #2: Simple Fight
35 / 57
![Page 36: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/36.jpg)
Example #2: Simple Fight
36 / 57
![Page 37: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/37.jpg)
Example #2: Simple Fight
37 / 57
![Page 38: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/38.jpg)
Example #2: Simple Fight
38 / 57
![Page 39: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/39.jpg)
Example #2: Simple Fight
39 / 57
![Page 40: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/40.jpg)
Example #2: Simple Fight
40 / 57
![Page 41: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/41.jpg)
Example #2: Simple Fight
41 / 57
![Page 42: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/42.jpg)
Example #2: Simple Fight
42 / 57
![Page 43: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/43.jpg)
Example #2: Simple Fight
43 / 57
![Page 44: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/44.jpg)
Inter-System Communication
Systems communicate by the means of events, only.
• no coupling between systems
easy to add or remove systems at any time
• great architectural advantage for general game features
need multiplayer? just send the events over the network!
need AI? just make it create events which are handled just like player input is!
need replays? just write all events with timestamps to a file!
44 / 57
![Page 45: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/45.jpg)
Inter-System Communication
45 / 57
![Page 46: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/46.jpg)
Advantages of Entity Systems
• update order is obvious
• components can easily be pooled and re-used
• independent systems can be updated by separate threads
• data can easily be serialized and stored in a database
46 / 57
![Page 47: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/47.jpg)
Disadvantages of Entity Systems (?)
• lookups cause performance hit
resist the urge to add cross-component references – this would make you lose all of the advantages mentioned before
just don’t flood your system with unnecessary component types – just as you would always do
• misbelief that it takes longer to “get the job done”
used at the InnoGames Game Jam #3 for creating a multi-platform multi-player real-time tactics game in just 48 hours –spending the little extra effort at the beginning pays off
o Always.
47 / 57
![Page 48: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/48.jpg)
Future Prospects
• Attribute Tables
store arbitrary key-value-pairs
used for initializing all components of an entity
48 / 57
<AttributeTable>
<Attribute keyType="System.String" valueType="System.Single">
<Key>EnemyHealthModificationComponent.EnemyHealthModifier</Key>
<Value>-3</Value>
</Attribute>
<Attribute keyType="System.String" valueType="System.String">
<Key>ActionComponent.Name</Key>
<Value>Take that!</Value>
</Attribute>
</AttributeTable>
![Page 49: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/49.jpg)
Future Prospects
• Blueprints
consist of a list of components and an attribute table
created with some kind of editor tool by designers
used for creating entites at run-time
49 / 57
<Blueprint><ComponentTypes>
<ComponentType>FreudBot.Logic.Components.ActionComponent</ComponentType> <ComponentType>FreudBot.Logic.Components.EnemyHealthModificationComponent</ComponentType>
</ComponentTypes>
<AttributeTable>
<Attribute keyType="System.String" valueType="System.Single">
<Key>EnemyHealthModificationComponent.EnemyHealthModifier</Key>
<Value>-3</Value>
</Attribute>
<Attribute keyType="System.String" valueType="System.String">
<Key>ActionComponent.Name</Key>
<Value>Take that!</Value>
</Attribute>
</AttributeTable>
</Blueprint>
![Page 50: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/50.jpg)
Future Prospects
50 / 57
StarCraft II Galaxy Editor
![Page 51: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/51.jpg)
Future Prospects
51 / 57
StarCraft II Galaxy Editor
![Page 52: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/52.jpg)
Future Prospects
• Hierarchical Attribute Tables
used for overloading blueprints with specific entity attribute values
e.g. reduced initial health
52 / 57
![Page 53: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/53.jpg)
Future Prospects
53 / 57
StarCraft II Galaxy Editor
![Page 54: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/54.jpg)
Future Prospects
public int CreateEntity(Blueprint blueprint, IAttributeTable configuration){
int entityId = this.CreateEntity();
// Setup attribute table.HierarchicalAttributeTable attributeTable = new HierarchicalAttributeTable();attributeTable.AddParent(blueprint.GetAttributeTable());attributeTable.AddParent(configuration);
// Add components.foreach (Type componentType in blueprint.GetAllComponentTypes()){
// Create component.IEntityComponent component = (IEntityComponent)Activator.CreateInstance(componentType);
// Add component to entity. this.AddComponent(entityId, component);
// Initialize component with the attribute table data.component.InitComponent(attributeTable);
}
// Raise event.this.game.EventManager.QueueEvent(FrameworkEventType.EntityInitialized, entityId);
return entityId;}
54 / 57
![Page 55: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/55.jpg)
Examples
Entitas - https://github.com/sschmid/Entitas-CSharp
55 / 12
![Page 56: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/56.jpg)
Examples
Tome - https://github.com/npruehs/tome-editor
56 / 12
![Page 57: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/57.jpg)
Takeaway
• inheritance-based game models show a lot of disadvantages
• entity systems are easy to maintain and debug
provide great extensibility without the necessity of modifying existing code
show better performance characteristics for both memory and CPU load
easy to implement commonly used features
o scripting
o serialization
o logging
57 / 57
![Page 58: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/58.jpg)
References
• Mick West. Evolve Your Hierarchy. http://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/, January 5, 2007.
• Levi Baker. Entity Systems Part 1: Entity and EntityManager. http://blog.chronoclast.com/2010/12/entity-systems-part-1-entity-and.html, December 24, 2010.
• Kyle Wilson. Game Object Structure: Inheritance vs. Aggregation.http://gamearchitect.net/Articles/GameObjects1.html, July 3, 2002.
• Adam Martin. Entity Systems are the future of MMOG development – Part 1. http://t-machine.org/index.php/2007/09/03/entity-systems-are-the-future-of-mmog-development-part-1/, September 3, 2007.
• Adam Martin. Entity Systems: what makes good Components? good Entities? http://t-machine.org/index.php/2012/03/16/entity-systems-what-makes-good-components-good-entities/, March 16, 2012.
• Scott Bilas. A Data-Driven Game Object System.http://scottbilas.com/files/2002/gdc_san_jose/game_objects_slides_with_notes.pdf, Slides, GDC 2002.
• Scott Bilas. A Data-Driven Game Object System.http://scottbilas.com/files/2002/gdc_san_jose/game_objects_paper.pdf, Paper, GDC 2002.
• Insomniac Games. A Dynamic Component Architecture for High Performance Gameplay.http://www.insomniacgames.com/a-dynamic-component-architecture-for-high-performance-gameplay/, June 1, 2010.
![Page 60: Game Programming 02 - Component-Based Entity Systems](https://reader035.vdocuments.net/reader035/viewer/2022062406/5594884f1a28ab077d8b45da/html5/thumbnails/60.jpg)
5 Minute Review Session
• What is an entity?
• Name a few drawbacks of inheritance-based game models!
• What is an entity component?
• What is a game system?
• How do game systems communicate with each other?
• Analyze the advantages and disadvantages of aggregation-based
game models!
• What is an entity blueprint?