Closed Auios closed 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 :)?
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.
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
Position
(where the tile is)Color
(self explanatory)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
Velocity
(to change the position, probably more component to control the velocity too but let's stay simple)Animation<Color>
(to animate the color)Damage
(the damage caused when colliding with this entity)Life
(the health points)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:
Map
component so your draw system knows it should show those entities firstLayer
component so your draw system can control what to draw in what order (background, map, objects, effects, ui, ...) (see the LayerSystem here for example)Now we don't need an exclusive rule anymore and our systems would looks like this:
Position
, Color
, Layer
] to draw everything layer by layerPosition
, Velocity
] and update both moveable platform part of the map and player/ennemiesPosition
, Damage
] and check for collision against [Position
, Life
], this can be used for tile damage, but also projectile, weapon, ... it doesn't care about the rest of the composition of the entitiesColor
, Animation<Color>
], again the entities can be from the maps or the player or ennemies, it doesn't careWant 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.
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! :)
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.
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