Doraku / DefaultEcs

Entity Component System framework aiming for syntax and usage simplicity with maximum performance for game development.
MIT No Attribution
658 stars 62 forks source link

Get set with only component #104

Closed Auios closed 4 years ago

Auios commented 4 years ago

A new method to get only entities with the specified components. For example: world.GetEntities().WithOnly<PositionComponent>(); Will get entities that have only the PositionComponent rather tan all entities with a PositionComponent

Doraku commented 4 years ago

I wondered about adding such an API but I didn't saw any use case so I just left it out. I am not against putting it in for real but I am really curious about what scenario would need this? While I did put in Without rules for simple exclusion, usually systems only care about the composition of an entity components in a non exclusive way. Would you mind sharing what you want to achieve :)?

Auios commented 4 years ago

To be honest. I just recently discovered ECS design. Your library is really my first experience with ECS (excluding a tiny bit of dabbling with Unity I suppose). So I am not entirely sure if I am even coding my thing properly.

I was just thinking about an optimization to the set size for the case where I only want to work on entities with a single component and exclude the rest.

Maybe a possible use case for things in the world that have a position and move but after a certain amount of time they "die" and become a part of the map. Map objects might only have the position component and I can make those entities a part of the map now by removing their life components. So now when I re-work the set. I can exclude previously living things and only iterate through things with a position.

Again, total newbie. That example could be totally wrong on various levels. Im just thinking it might be a way to reduce the number of entities in a set as an optimization.

Doraku commented 4 years ago

I was just thinking about an optimization to the set size for the case where I only want to work on entities with a single component and exclude the rest.

From my understanding of ECS this kinda goes against the philosophy of the design: making scalable independent system logics based on entities component composition. If your system cares that your entities can only be processed by him, it doesn't feel so independent anymore.

Let's take your map example, for simplicity let's say it is a tile based map. Your component would be

Those are generic enough that your player/ennemies would have the same components, but you want to draw the map first and then your moveable objects, hence why you want the exclusive WithOnly.

What if you want your tile to move in the map (moving platform), what if you want them to change color by an animation, what if you want to make it hurt whatever stay on the tile? You would need all those new component

You can't just request an exclusive [Position, Color] anymore as you still want those tiles to be part of the map and draw first. You can have different strategy:

Now we don't need an exclusive rule anymore and our systems would looks like this:

Want to add a time to live on entities? Add a new component TimeToLive and a TimeToLiveSystem which take [TimeToLive], once the period has expired, it can dispose the entity or remove whatever component you don't want anymore (maybe TimeToLive has an Action<Entity> value to be called once expired).

Hopefully you see now why I am hesitant about the need for an exclusive composition rule.

Auios commented 4 years ago

So I believe I've been approaching my development/concepts on ECS incorrectly then. I don't think I have any more questions or concerns now at the moment. This helps me understand more clearly now about how I should be structuring my application. I appreciate the insight and fast response, thanks! :)

Doraku commented 4 years ago

No problem :) it is a complete different approach to OoP after all. I really like the way you can add systems and components without breaking your existing features nor needing to rethink the inheritance of your types but the change of mindset can take some time.