5
\$\begingroup\$

I'm new in game development and I'm struggling to implement Entity Component System (ECS) properly, I have no idea whether I'm doing it right or completely wrong.

Basically, I try to implement ECS pattern with SceneGraph system, so here my current understanding:

  1. Entity is nothing but a components bag, it has no logic but it may (or may not?) contain essential information to differentiate from the other Entity or grouping them as well, such as ID, Name, Tags, etc.
  2. Component is a data container but contains no logic how to process the data but may contains function to manipulate the data, for example a Transform component may contains Inverse() function.
  3. System is the implementation holder, it process Entity based on its Component. The System may define a "requirement". The Entity is processed if it match the requirement, for example SpriteRendererSystem only process Entity that contains SpriteRenderer and Transform component.

Currently, I have no idea if it's correct or completely mess. Therefore, I try to implement ECS along with SceneGraph as stated before, here my SceneGraph implementation:

  1. Node is an object in the Scene, basically, everything that need to added into the Scene must implement this. The Node can add another Node instances as its child as well as parent. Currently, my Entity implement Node, meaning that all Entity can be added into Scene
  2. Scene is the top most level of Node, it represent a root of Node and there can be only one active Scene at a time. The Scene basically implements Node and it has Update() and Render() functions, this functions is called by SceneDirector
  3. SceneDirector is basically inherit game window, it manage the active Scene, passing Render() and Update() method to the active scene as well as updating the Input states to the Scene.

Now, I combine both pattern as follows:

  1. The Scene has a list of System as well as list of Node. Depending the System, the System may called on Render() function if the System is Renderable, otherwise it will called on Update() function
  2. Upon Render() / Update() function, the Scene will loop the entire Entity, it also loop the entire registered System. The System will check the Entity whether the requirement is match, if the requirement is fulfilled, the Entity will be processed by the System

Now my questions:

  1. Can Component hold the logic? or it should not and stay as a bag of data?
  2. Is it correct implement Node on Entity?
  3. Is it correct to let every instances of Scene has their own System? or should I make it globally by storing the list of System in SceneDirector?
  4. Is there any more efficient way to process the Entity rather than loop the Entities and the Systems completely?
  5. Is my understanding correct about the ECS? Am I on the right track? or completely wrong?

Thanks in advance

\$\endgroup\$
1
  • \$\begingroup\$ The whole point of ECS is that an entity is just an integer, and components are arrays, and to get the data for a particular entity you index into the component with the entity. 'System's are just loops over components. \$\endgroup\$
    – mrr
    Commented Dec 13, 2016 at 22:08

2 Answers 2

10
\$\begingroup\$
  1. Can Component hold the logic? or it should not and stay as a bag of data?

That's how you like. I suggest to stick with the first implementation and finish your project, then see if it worked out for you or not.

If you do that, however, I suggest you drop the concept of System as you described it, as you'll have two lanes of doing things, which will, in the long run, add confusion ("where is the code that does this?").

Note that Unity has components where the logic is done, and no system in the way you described it.

  1. Is it correct implement Node on Entity?

I don't think so.

You could have an entity which is not in your scene graph, or components that do not care at all where they are positioned (either in the world, or in the scene graph).

Think about composition over inheritance. I see ECSs as a tool which is parallel to a scene graph: an entity could have a Node component which would refer to a node in your scene graph. And a physics component which would hold a reference to it's physics representation in your physics engine.

This way, your systems are loosely coupled, and it favours reuse.

  1. Is it correct to let every instances of Scene has their own System? or should I make it globally by storing the list of System in SceneDirector?

IMHO The System and the Scene should be two parallel structures, so you should not store the System in the Scene.

  1. Is there any more efficient way to process the Entity rather than loop the Entities and the Systems completely?

Efficiency depends a lot on your implementation and your needs. I suggest you implement your architecture, work with it while keeping your classes as much decoupled as you can, and profile it often. If you find bottlenecks while profiling, fix these, and not non-existent ones. Maybe your rendering or your physics will be your bottlenecks, and not your ECS implementation.

  1. Is my understanding correct about the ECS? Am I on the right track? or completely wrong?

Here is what an ECS is:

enter image description here

(Image source)

By this, I mean that every dev and every company has its own view of what an ECS is or should be.

Work with something you understand, keep your code clean, and as soon as you notice that there is something that's not right, stop, think about (but don't overthink about it), fix the issue, and carry on. Your architecture will evolve with your project and yourself as a dev.

And finish your project. Your second project awaits, and will be much better structured :)

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Oh uh, i cant upvote this due to my low rep. Thanks for your answer, it clears me out, however it left me a question behind. Where should I store the sets of System that could be processed by current active Scene? in the SceneDirector? but since it should run parallel, it would be make no sense if I put in the SceneDirector.. or additional class like SystemManager? \$\endgroup\$
    – tronic
    Commented Dec 8, 2016 at 1:12
  • 1
    \$\begingroup\$ @tronic Yes, you have an object which holds the systems. It can be your top object, or a manager like SystemManager. \$\endgroup\$
    – Vaillancourt
    Commented Dec 8, 2016 at 11:05
2
\$\begingroup\$

I concur with Alexandre Vaillancourt on all except the 4th point.

  1. Is there any more efficient way to process the Entity rather than loop the Entities and the Systems completely?

The goal with Systems is to process a specific set of components not necessarily the entity itself. The trick is to register an entity with a system only when it has the components required by the system.


I'd recommend reading the following articles. They give a solid overview of ECS architecture:

What is an entity system framework for game development?

Why use an entity system framework for game development?

\$\endgroup\$
1
  • \$\begingroup\$ That are some great articles! Thanks for sharing! \$\endgroup\$
    – FutureCake
    Commented Oct 5, 2019 at 22:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .