Add World::entity_scope
to split the borrow on the World
by extracting a single entity
#13128
Labels
A-ECS
Entities, components, systems, and events
C-Usability
A targeted quality-of-life change that makes Bevy easier to use
D-Complex
Quite challenging from either a design or technical perspective. Ask for help!
S-Ready-For-Implementation
This issue is ready for an implementation PR. Go for it!
What problem does this solve or what need does it fill?
When working with avatar-centric games (like platformers or FPS games), one object (the player avatar) is typically wildly more complex and important than others. Writing exclusive systems that fetch "the player" and operate on it in a game-object style can be an effective programming pattern.
In order to make this work, we often want to access that highly important entity, and relate it to "everything else around it". The current most obvious pattern is to fetch that data, clone or remove it out of the world, and then operate on it.
What solution would you like?
Add
World::entity_scope
, which works likeresource_scope
, but returns anEntityMut
instead of a resource, in addition to a reference to the rest of the world.This could be done by removing and then re-adding the entity but:
Entity
id of the object should remain unchanged.A strong form of entity disabling (#11090), which makes it fully impossible to access the data of the object would be a clean way to do this, but would have more significant architectural implications. This would also come with less caveats: being able to add children to the requested entity without immediately panicking would be great.
What alternative(s) have you considered?
An
EntityWorldMut
would be strictly more powerful, and makes more conceptual sense: being able to freely add and remove components would be really useful. However, actually providing that access is fundamentally unsound with the archetypal ECS that Bevy uses: changing this data requires changes to the metadata of the component storage itself, and requires a true hard lock.Users could manually implement this pattern, by despawning and then spawning the player object, but without a way to clone entities (#1515) or otherwise extract all of their component, actually doing so is tedious.
Furthermore, any existing references to that entity (such as
Parent
components!) will break, since theEntity
ID will be changed.Change detection will also be triggered on every component, effectively rendering it useless for their game object style entity.
Additional context
This is a conceptual sister to #13127: both are in the service of making the "get and work with a single entity in a loosely typed way" workflow easier.
Performance is always nice, but is a secondary concern for this style of API.
An equivalent
hierarchy_scope
, which extracts a whole tree of entities, is probably useful down the line as well: player objects are often conceptually composed of complete trees in order to support complex animated models.The text was updated successfully, but these errors were encountered: