Entity Component System
September 3rd, 2024
Hello! I wanted to give a run-down of the Entity Component System (ECS) for Dribble King.
What is an ECS?
An ECS is a design pattern used to represent game objects and give them similar functionality, in components. Each object in the game world will be an entity, while its components will extend the entity's base functionality. This keeps the code modular and makes it easy to add/remove features from entities. ECSs are implemented in popular game engines like Unreal Engine and Unity.
My Implementation
Let's start with a few global variables and functions:
The MAX_COMPONENT will be used to initialize a few arrays in future classes. The GetComponentID assigns IDs to each unique component using the componentIDcounter.
Component
Here is the Component class. It is a simple base class that has a few virtual functions and getters for convenience. Child classes will have much more to them:
Entity
As for my Entity class, this is where things get interesting. I'll start by introducing the members:
The bitset is used to quickly tell if an entity has a certain component. The unordered_map stores each component in a shared_ptr to manage the memory. The key for the map is each component's ID. I chose to store the components in a map for the quick insertion/removal time and quick lookups. I'll explain the mIsActive variable when it comes up later, just know it's set to true in the constructor.
Here, we have some virtual functions which call Update and Render for all its components.
An entity should be able to add components to itself:
After the component is made, we store it in the map and set its bit to true. After setting up its two members, we call the component's Init function and return it. Here are a few functions for accessing the component:
EntityManager
With these classes ready to go, we'll move on to the EntityManager class. It is responsible for creating, updating, and keeping track of all the entities in the game. It stores these entities in a vector:
It's Update, Render, and MakeEntity functions are very similar to the Entity:
Unique to the EntityManager is the Refresh function:
This is where the IsActive function becomes relevant. Any inactive entities will be removed from the EntityManger's mEntities list. And because the entities are smart pointers, the memory will be freed automatically!
Conclusion
So far, I am happy with this system. Right now, I have created Transform, Sprite, and Collision components that all work well. I plan on looking at other game engines for reference on how I can improve the system even more!
Thanks for reading,
Jamari
Dribble King
Status | In development |
Author | MrMisinput |
Genre | Action |
Tags | Top-Down |
More posts
- Clock Class and Player Boost49 days ago
- Keyboard Controller49 days ago
- Render Layers57 days ago
- Animations57 days ago
- Collision Part III: Discrete Collision64 days ago
- Collision Part II: Sweep and Prune Detection65 days ago
- Collision Part I: Manager and Component66 days ago
- Project Introduction72 days ago
- Design Doc Link81 days ago
Leave a comment
Log in with itch.io to leave a comment.