Ralith / hecs

A handy ECS
Apache License 2.0
967 stars 82 forks source link

Queries that add/remove components #334

Open Ralith opened 1 year ago

Ralith commented 1 year ago

Common patterns involve iterating over a query and adding, adding if absent, or removing a component from every entity. This presently requires a somewhat awkward and suboptimal dance where operations are buffered in a CommandBuffer or moral equivalent and executed in a separate pass after the query completes. We could extend the query machinery to support these patterns directly with query transformers that allow components to be directly written to or consumed from archetype storage, though considerable care will be required to soundly handle corner cases including incomplete iteration, leaked queries, and failure to insert all expected components.

Design notes:

Ralith commented 1 year ago

A straw implementation might place a CommandBuffer in the query iterator that gets executed on drop.

One significant limitation is that such queries must take &mut World, and hence cannot contain random access to unrelated entities.