Implementing an Entity Component System in C++

Started by
13 comments, last by GuiTeK 10 years, 1 month ago


I like my architecture to be clear, not to be a mix of several design patterns which are supposed to be "contestants" (e.g. Map would be a traditional class whereas Character would be an entity). It's quite confusing I think.

Not at all. The cleanest architecture would likely be one where you use the appropriate pattern where it makes sense. Design patterns aren't "pick one".

Advertisement

I am sort of late to this discussion but I am looking on how to implement the pattern myself,

Ok thanks for your answers.

I have another trouble:

  • My game has maps
  • Maps are made of cells
  • Cells have a position, an ID and a texture.

What should be the components, what should be the entities?

I'm pretty sure Map should be an entity, but then:

  • Cell can't be an entity (otherwise I can't use it in my Map entity)
  • Cell can't be a component since it uses other components (Position, ID and Texture).

And the same problem goes for a lot of things. Can one use component in other components or did I get it all wrong?

From what I've gathered I would say neither of those are entities, they all should be part of a map resource, referenced by a map component that is updated by a map system.

To elaborate, your map may be a XML document with cell or tile elements themselves with position (relative to the origin), id and texture (itself a reference to an image, shader and/or material) attributes.

You write some code to convert the XML into a runtime resource object, which is then referenced by a map component, the component is the "instantiation" of your resource, and it will then contain information specific for that instance of the resource, for example position if your map may coexist with multiple maps snapped together.

Later on, in your game loop you may have a map system which updates any variables in your map component, and a rendering system may render it later, or a collision system may query the component which itself would query the resource for collision information, etc.

Ok, so I wonder how I should implement the systems. Since there is no reason to instanciate them (it would make no sense), the methods should be static, don't you think? Or better, I could use a singleton to make sure there is only one instance of each system.

Honestly, introducing singletons or static anything in this the mix is a recipe for trouble. You're only making the solution far more rigid and brittle at the same time which will be inflexible and hard to maintain long-term.

Additionally, systems should be instantiated because they'll likely need to maintain internal state and not introducing singletons or static state implies you can easily run two copies of the system on perhaps different entity manager data sets or different game states and perform multiple simulations in parallel or sequentially without any trouble.

Still, I hesitate to implement it in my game for the reasons below:
I like my architecture to be clear, not to be a mix of several design patterns which are supposed to be "contestants" (e.g. Map would be a traditional class whereas Character would be an entity). It's quite confusing I think.
With the OOP approach, the "skeleton" of the game is well defined by the classes: you read the Character class and you know everything about it. Whereas with the ECS approach, a Character is divided in several files (1 entity, x components and x systems) and you don't know where it all gets together. However, I agree the code is more modulable with an ECS.
So I think for now I'll stick with the "old" OOP approach. I'm sure it can work flawlessly as a lot of games don't use an ECS and they work well.

A clear architecture has nothing to do with the design patterns which it uses. In fact, an architecture tends to be cleaner when the right design pattern is chosen for the problem at hand rather than trying to shoehorn something that doesn't fit due to some bias or other factor. Opting to use design pattern A for part of a problem and design pattern B for another portion with some design pattern that marries the two is actually very common place in programming and in my experience generally carries considerably more benefits than it does consequences.

I prefer to consider that both a Map and the Player are stand alone classes pertinent to the game engine, core classes if you will. I then give the Player class an unsigned integer property that is basically the identifier to some game object in the game object system that represents the actual player. The benefit here is that if the engine operates on the exposed API of the Player class, the engine doesn't care if its an entity or not, nor does it care about the components which make up the player. With all that abstracted, you can change implementation details of the Player with minimal impact to the engine/game itself.

And as you can see, such an approach does follow your idea of a Character class that knows everything about itself. The only difference is that rather than the state of a Character being stored in that class specifically, the Character class queries various systems to get it's state based on the method call invoked.

One of the biggest reasons why these systems are great is the focus on data-oriented design. You store data that you plan to operate on at the same time together in logical chunks, utilizing cache friendly operations, thus boosting performance. Because you're grouping data oriented things together and decomposing state, you also gain the benefit that it becomes easier to piece features together and morph object state from A to B. Of course, all this implies a different mindset to understand that you're updating game state in many many tiny stages throughout a single update tick for any given object.

But nothing of the above says you cannot use ECS in conjunction with OOP. Both are wonderful tools that can and do work well together if you follow the simple rules of programming with separation of concerns, single responsibility principals and data-oriented design goals.

Once again, thank you for your answers, it really helps me and it makes things clearer.

I'll try again to implement a proper ECS with OOP smile.png .

This topic is closed to new replies.

Advertisement