EnTT: An Introduction to Entity Component Systems for Game Engines

In this exciting installment of our Game Engine series, we are going to explore EnTT, an Entity Component System (ECS), and how it can be integrated into our game engine. If you are not familiar with ECS, I highly recommend watching the previous video where I explained the concept and its advantages over traditional approaches.

Today, we will specifically focus on EnTT, a widely used ECS solution. You can find the GitHub repository for EnTT in the description below. I have chosen to use EnTT for several reasons – it is a well-established and efficient ECS that has been used by major projects such as Minecraft. Additionally, writing a robust ECS from scratch is time-consuming, and as a solo developer, I have to prioritize my efforts.

To get started, we need to include the EnTT library in our project. Luckily, EnTT provides a single header file, which makes it incredibly easy to integrate. Simply download the entt.hpp file from the GitHub repository and save it in the appropriate folder within your project. In my case, I have created an include/ent folder and saved the file there.

Now, let’s dive into some code! We will start by creating a scene class, which will serve as the container for our entities and components. The key component of EnTT is the registry, which is responsible for managing entities and their associated components. To create an entity, we call registry.create(), which returns an entity identifier. We can then use this identifier to add components to the entity using registry.emplace<>().

Components in EnTT are simple structs that hold data. For example, we can create a transform component for managing the position, rotation, and scale of an entity. Additionally, we can create other components such as a mesh component for handling mesh-related data. Once we have defined our components, we can easily add them to entities using registry.emplace<>().

To access and manipulate components, we can use the registry.get<>() function and pass in the entity identifier. This returns a reference to the component, allowing us to modify its data. If we want to check if an entity has a specific component, we can use registry.has<>(). This is particularly useful when iterating through entities that possess certain components.

Further reading:  Make Your Desktop Apps Stand Out with Walnut

Speaking of iteration, EnTT provides a convenient way to iterate through entities with specific components using views. A view is essentially a filtered subset of entities that have the specified components. To create a view, we call registry.view<>() and pass in the component types. We can then iterate through the view and access the components of each entity.

EnTT also offers advanced features such as event-based systems and runtime views. We can connect functions to specific events, such as when a component is created or destroyed, allowing us to perform custom logic. This can be especially useful for handling scripting events or performing additional operations on components.

In conclusion, EnTT is a powerful and efficient ECS solution for game engines. It provides a straightforward way to manage entities and their components, making it easier to develop complex systems. With EnTT, we can efficiently iterate through entities, access and modify components, and handle events. By integrating EnTT into our game engine, we can unlock new possibilities and enhance our development workflow.

So, why not give EnTT a try in your next game engine project? For more information and detailed documentation, make sure to check out the official EnTT GitHub page. Happy coding!

Techal

YouTube video
EnTT: An Introduction to Entity Component Systems for Game Engines