• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
GuiTeK

Implementing an Entity Component System in C++

14 posts in this topic

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.

0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites


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

1

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites
 

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.

 

2

Share this post


Link to post
Share on other sites

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 .

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now