Entity component system in games – Starter Guide
Introduction
Entity Component System is a software architectural pattern. You may have heard that software architecture is important. Let me show you how simple this makes your game logic.
Imagine building a house in a hurry, with no plan or blueprint.
If you’re hungry like me, you’ll start with the kitchen and put the bathroom nearby for convenience. After finishing up in the bathroom, I like to relax and watch some TV or play a game in my room.
So here’s my hastily created house. What do you think?
As you can see, having no plan or design leads to losing important details.
Software architecture serves as the foundation that builds great software. It considers everything you’ll need and keeps it flexible for future changes.
What is the Entity Component System pattern?
Entity Component System uses a data-driven design that separates data from its logic. Data-driven design means that you can create your objects from its data. For example you could include images, 3D models, position in the world, and so on as data.
Entity
Entities act as the unique container for various properties and components. An example of an entity is the Player, game characters, and world objects. The entities id is used to gather its data and components by a system.
Component
Components have specialized properties that an entity can gain. An animation, visual effect, or whether an object should have physics would be a component.
System
A system has a single purpose that handles the logic for a specific set of components. An Input system will update character positions from your keyboard or mouse. A Physics System handles collisions. A Render system will take all available entities that have a Sprite component and draw them as you see fit.
Why should you use ECS?
Old patterns
Before ECS, games would use plain inheritance. Inheritance allows us to share code with other classes. While sharing is great, it so also makes game logic bloated and difficult to understand when misused.
For instance, you may have one kind of character that can only fly and another kind of character that can only swim.
What if there’s a magic spell that allows any character to fly?
Now you must rethink your design to make your development easier. These difficult relationships are no longer needed.
ECS pattern
By pooling these relationships together, ECS dissolves the headaches of a knotty class structure.
Design flexibility
Entity Component System gives you great flexibility to craft your entities without having to update unrelated logic or systems.
ECS libraries to use in your game
Listed below are some example frameworks to get started with using Entity Component System.
Javascript
Rust
C/C++
Lua
Python
Continue on your journey and find some useful gamedev tools from itch.io!