Presentation is loading. Please wait.

Presentation is loading. Please wait.

Entity Component System

Similar presentations


Presentation on theme: "Entity Component System"— Presentation transcript:

1 Entity Component System

2 Composition over inheritance
An alternative to OOP inheritance e.g. the Component structure of Unity Avoids Java-like inheritance trees e.g. Doom 3 (from “One Weird Trick to write better code” idClass idEntity idAnimatedEntity idWeapon idAFEntity_Base idAFEntity_ClawFourFingers idAFEntity_Vehicle idAFEntity_VehicleFourWheels idAFEntity_VehicleSixWheels idAFEntity_Gibbable idAFEntity_WithAttachedHead idActor idPlayer idAI Why is this bad (not everyone would think it’s bad…)? See for more...

3 In ssuge We’ll have a GameObject class
Contain a transform (SceneNode) Can be parented (again, SceneNode) Other info (e.g. tag, resource group, etc.) We’ll have a base Component class An abstract class – meant only to be a base We’ll derive multiple types of Components from it MeshComponent GUIElementComponent ParticleEmitterComponent When setting up our scene, we can “snap-in” a component to add functionality to a GameObject

4 C++ Polymorphism What is the output? class Yarg { public:
void func() { cout << “Yarg” << endl; } }; class Tango : public Yarg { public: void func() override { cout << “Tango” << endl; } }; optional, but nice (like annotation in Java) Yarg * y = new Yarg(); Tango * t = new Tango(); y->func(); t->func(); delete y; y = t; // Valid b/c of POLYMORPHISM

5 A fix… You can make a method virtual: Syntax: Now it’ll work “better”
Makes the compiler wait until run-time to determine the method to call. Adds a virtual function table to the object (a little overhead) Syntax: Now it’ll work “better” class Yarg { public: virtual void func() { cout << “Yarg” << endl; } };

6 Pure virtual You can choose to not implement a method in the base class …and force the child class to define it. A little like Java interface classes The base class is now abstract We can’t make instances of it. We can, however, use polymorphic pointers of the base type Syntax: class Yarg { public: virtual void func() { cout << “Yarg” << endl; } = 0; };

7 Why? In our ECS, I want to have a collection of Component pointers
But…they’re not actually pointing to Components …instead they’re pointing to MeshComponents, etc. Design Decision What kind of collection: array (fixed size or not?), map, vector? Duplicate component types? A createXYZ method in GameObject vs. an addComponent(Component*) method.

8 Factories Another common design pattern
Used to create instances of another class In C++ Could be a separate class (e.g. MeshComponentFactory – probably a Singleton) a static method (e.g. in the MeshComponent class) Advantages: Can optimize memory allocations [side-topic]: memory fragmentation and new operator Isolates code to create instances of that class to one spot (for us) could update all components “in parallel”


Download ppt "Entity Component System"

Similar presentations


Ads by Google