What do I need to know in order to implement ECS in a video game? I'm developing a video game and so far I wasn't using any complete architecture, I was just using inheritance but I don't like it because the code became messy. So far, I've seen so many articles in which the subject is treated, but I don't understand how to implement it. All what I get is there is that a component is a bunch of information, an entity is a bunch of components, and each system deals with the logic of its own components. But I've got a question about it, they're a bunch of IDs, how do those IDs store data? How to retrieve data from it? I've got a problem, since I was trying to implement components, in order to do it all what I did was declare a parent abstract class in which I'd store members of that class and then I would be able to access to those members. The problem came when I was trying to declare children of that abstract class, since that parent class can't have all the members of all the another components (and I need a way to iterate all the components in a vector). So... I want to keep the things as simple as possible. Any advice for someone like me? How can I implement ECS in a simple platformer? Please, Thanks in advance!
Keep in mind that ECS is at its core not an object-oriented design pattern. It is more suitable for structures and functions.
[entities are] a bunch of IDs, how do those IDs store data?
In an ECS-by-the-book architecture, entities do not store any data. Components store data. Also, neither entities nor components contain any logic. All logic is in the systems.
So you don't have an entity with a collision box. You have a vector of collision box components and some data structure which maps IDs to collision boxes (the article mentioned in the question calls these "Component Managers"). What kind of data structure is an optimization problem. But a good default solution is to use
std::map<int, CollisionBox*>. You then have a CollisionSystem which detects and handles intersections between those collision boxes.
Sometimes it can be useful when components contain the ID of the entity they belong to so you can more easily find other components of the same entity. But not all components will need this.
However, this is a very C-ish style of creating an ECS system. If you feel more comfortable with a more object-oriented design, then you can also have a
class Entity which contains pointers to all its components. You can even have a class hierarchy of entities which differ by what components they have and how they initialize them.
But the concrete instances of the components should be stored in large arrays (which you can abstract as a
std::vector if you like) with one array for each type of component. Systems should operate on these arrays whenever possible to benefit from memory locality (iterating through an array is much faster than dereferencing a pointer).
Just don't forget about the basic principles of ECS:
- Components contain all the data
- Systems contain all the logic
- Entities say which components belong to each other
Entities don't have to be a bunch of ID's. Just create an Entity Class that will contain a list of components and that will have some methods that will help you add, remove, get, and check if the entity has certain components.
For component, just make an empty interface called component and use it for polymorphism. That way make more classes that extend Component and store data in them. Put them in the list of components that you have in your entity.
Then make systems. The way systems work is, you give your entity to your system and the system checks if that entity should be operated in that system by checking if the entity has components that the system requires. If yes, retrieve the components from the entity and make the logic in some kind of an update method that will run every time your game updates!
Also, you asked how the ID can store those components. Well, it doesn't, but in your engine class, you just store it in a HashMap. The hash map has the entity ID as a key and the List of components as the value, but as I said, better just make an Entity class that will contain the ID and the list of components! It's easier
There are many ways you can do this, this is just how I did it. There is also one tutorial series on Youtube by BennyBox who makes a 3D engine in C++. There he uses ECS and he even programs the ECS system there. You can do the rest of the stuff by your way and just copy his ECS if you don't want to make your own or the way I explained.
Here is the link: