Who should add the "entities" to the scene?

Hello there!

Following ECS and other design patterns, I’m wondering who’s the responsibility of adding an entity to the scene, into the right container? A “controller”? :eyes:

Speaking of THREE.js, an Object3D should be added ( .add() ) somewhere. Maybe in the root scene, maybe as a child in another object.

For example, let’s say an EnemySpawnerSystem decides to spawn a new enemy / entity. How should this system know where to add it to the scene? :eyes:

Do you have an opinion about this?

Hi @endel

It really depends on how you want to structure your code. One idea could be to do it explicitly having a Parent component with the reference to the object3D and then you will have a system that will check for that attribute to be added or changed and just attach it to the desired object3d.

Another option is doing it automatically, like if you know all the enemies will belong to an specific parent Object3D, as long as you create a new enemy the system will add it to that object3D.

Or a mix of both, where you can have a query like: [Enemy, Not(Parent)] and the system will add a Parent component to the Object3D that should hold all the enemies, and after that the query won’t be satisfied, so it will act as a factory pattern.

Systems can and should handle adding entities (and adding/removing components on them). This way you aren’t shoehorning another design pattern into ECS.

I suggest having a factory module for each kind of entity that exports a create(name) method. That way you can easily create tons of entities from the same template in multiple places.