Jump to content

  • Log In with Google      Sign In   
  • Create Account

Implementing an Entity Component System in C++


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 GuiTeK   Members   -  Reputation: 140

Like
0Likes
Like

Posted 22 February 2014 - 08:52 AM

Hello,
 
I've recently discovered the Entity Component System (ECS) and indeed, it seems to be a good design pattern for games.
My game is still pretty small, but there is already a great number of classes. That's why I'm planning on refactoring my whole code to implement an ECS.

I've read several articles and tutorials about ECS, I think I have a good overall comprehension of the pattern, but I have some questions about the implementation.
 
This is how I see it:

  • An abstract class (interface) Component from which all components inherit from.
  • An Entity class. Each instance stores an array of Components*. Components can be added to an entity through the addComponent(Component*) method. This method also attaches the entity to the right systems (e.g. if we add the component Position, the entity is attached to the system MoveSystem).
  • An abstract class System from which all systems inherit from. As described above, each system keeps an array of the entities it should work on. Finally, each system has an update() method to update the entities.

So far, is it correct?

 

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.

What do you think?

 

 

Thank you.



Sponsor:

#2 ultramailman   Prime Members   -  Reputation: 1571

Like
9Likes
Like

Posted 22 February 2014 - 12:36 PM

I dont think you need to make them singletons. In my mind, systems are more like functions that do stuff given entity inputs, so even plain old functions are fine, no need for a class.

I cant say much about using OOP constructs approach though, Ive been using the "relational database" approach to ECS. Take a look at that, it might give you a different perspective on what ECS is.

#3 ankhd   Members   -  Reputation: 1297

Like
1Likes
Like

Posted 22 February 2014 - 07:18 PM

hi.

You dont really need to put them in a array, I just place them in as a  object and have my entity class have virtual functions like GetComponentMotion and the likes.

I do this so Im not always polling a array to find the component. See if you have 20 components it could be the last one you want and thats 19 time through the array

just to find some thing.



#4 GuiTeK   Members   -  Reputation: 140

Like
0Likes
Like

Posted 23 February 2014 - 06:56 AM

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?



#5 Juliean   GDNet+   -  Reputation: 2663

Like
7Likes
Like

Posted 23 February 2014 - 07:43 AM


Cell can't be a component since it uses other components (Position, ID and Texture).

 

Sure, why not? You can make a cell-component which handles all the cell-specific logic, but also require each cell to have a position, id, and texture component. Is there any reason why you can't do that?

 

But in all honesty, I wouldn't even make cell eigther an entity or component. Is there any reason why those have to be part of the entity-system? I would program the whole map/grid-stuff as if there where no entity-component system, and then make a map-component that instantiates a map-class and offers all needed interaction with the ECS.



#6 phil_t   Crossbones+   -  Reputation: 3928

Like
3Likes
Like

Posted 23 February 2014 - 09:42 AM


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.

 

Why does instantiating them make no sense? Surely you have some non-global state that various systems need to do their job.

 

There's really no reason to make the methods static, or to make them singletons. It is a limitation and gains you nothing. Just instantiate the systems you need, and add them to some explicitly-ordered list in your "world manager" (or whatever) so it can go through them and Update them.

 


An Entity class. Each instance stores an array of Components*. Components can be added to an entity through the addComponent(Component*) method. This method also attaches the entity to the right systems (e.g. if we add the component Position, the entity is attached to the system MoveSystem).

 

That's one possible implementation. Another is to store each Component of a particular type in an array of that type, and then the entity exists simply as a mapping to an index in the individual Component arrays. There are many different ways to store them, depending on the sort of cache performance or flexibility you need (for instance, do you allow for more than one component of a particular type on an entity?).



#7 phil_t   Crossbones+   -  Reputation: 3928

Like
3Likes
Like

Posted 23 February 2014 - 10:02 AM


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

 

I don't think I've ever seen an ECS that allows for putting components inside other components. But, there are other ways to create a hierarchy. For instance, if you had a character (an entity) that was holding a sword (another entity), and the sword's position needed to match the character's, you could have a TransformChildren component on the character entity that contains a list of child entity ids. Then you would have a system that operates on all the TransformChildren components and updates the child entities with the correct position. You can create all sorts of hierarchies this way: visual, team, ownership/inventory (e.g. a character has a box entity in his inventory, and that box contains other entities).

 


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).
 
So now you can better answer this question. There's no reason that both map and cells both couldn't be entities. Whether or not that actually is a good idea would depend on the nature of your game. As Juliean said, the map maybe doesn't need to be part of the ECS at all (sure, some of the game systems probably need to be able to query the map to find out what kind of cel is at a particular location - but that doesn't mean the map/cells need to be an entity).
 
What do you gain by making cells be entities? Can cells change position and change texture? If so, then that might suggest they should be entities. But if this is just a static map with a huge number of cells laid out in a predictable pattern (e.g. a grid), then I really don't see any reason they should be entities.


#8 haegarr   Crossbones+   -  Reputation: 4332

Like
6Likes
Like

Posted 23 February 2014 - 10:10 AM

Don't make the mistake to fit all and everything into the CES schema. It is as wrong as trying to fit all into a scene graph. Notice that another word for entity in the given meaning is "game object".

 

You haven't said what a Map is precisely. Probably it is a game level, i.e. a description of which game objects are to be loaded (perhaps when they should be loaded), and how to parametrize them if instancing game objects is implemented this way. IMHO such a thing isn't a game object at all.

 

Another mistake would be to not allow interconnection between entities. Parenting / forward kinematics / 3rd person camera attachment is just one thing that expresses interconnection. E.g. a weapon is its own game object, regardless whether it lies on the ground or is held by a character. Interconnections between components is also necessary. E.g. the Placement component stores the world position and orientation of an entity, and several other components depend on this.

 

 

EDIT: Oh, I see that I just second phil_t's posts. However, it's nevertheless my opinion.


Edited by haegarr, 23 February 2014 - 10:12 AM.


#9 GuiTeK   Members   -  Reputation: 140

Like
0Likes
Like

Posted 24 February 2014 - 05:21 AM

Thanks for your answers, now I understand a little better the whole thing.

 

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.



#10 haegarr   Crossbones+   -  Reputation: 4332

Like
6Likes
Like

Posted 24 February 2014 - 05:54 AM

The following is my personal opinion, of course...

 


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.

A big No! The toolbox of a programmer should have more tools than a hammer. You ever should use the solution that is appropriate for the given problem.

 


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.

Look at the traditional scene graph like in OpenSG. IMHO it is a bad solution (is not meant disrespectful) just because they tried to fit all into the hierarchy of the graph structure. Look at the mega super class approach where tons of functionality is shifted into the base class just for the case that the one or other heir will need the one or other functionality. Look at the spaghetti inheritance approach which is problematic to maintain n the long run. (BTW, the latter two are the reason why CES is so popular these days.)

 

For a simple game the bad effects will not be so visible, and things can still be maintained. The problems will arise more and more when the game grows. Of course, also CES is not the ultimate solution for such problems, but it is better suited than the other both approaches mentioned above. And, as said, also let composition not be your sole hammer; inheritance has still its justification.



#11 phil_t   Crossbones+   -  Reputation: 3928

Like
1Likes
Like

Posted 27 February 2014 - 10:38 AM


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".



#12 Kwizatz   GDNet+   -  Reputation: 1200

Like
1Likes
Like

Posted 27 February 2014 - 04:03 PM

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.



#13 crancran   Members   -  Reputation: 415

Like
2Likes
Like

Posted 27 February 2014 - 05:03 PM

 

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.

 



#14 GuiTeK   Members   -  Reputation: 140

Like
0Likes
Like

Posted 03 March 2014 - 03:07 AM

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 .






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS