Allow World::entity family of functions to take multiple entities and get multiple references back#15614
Conversation
|
Is is possible to deprecate the existing methods rather than deleting them outright? |
Yes, I can do that. |
djeedai
left a comment
There was a problem hiding this comment.
Very neat change and very good quality contribution. Not an ECS expert so maybe need a review from an SME, but otherwise looks good to merge.
|
Forgot to switch the label :) |
There was a problem hiding this comment.
I quite like this, although I don't love the fact that get_entity (singular) can now be used with multiple entities. How would you feel about keeping get_entity as a way to get one entity, while using the new WorldEntityFetch stuff with get_many_entities instead?
Personally, I think that's reasonable, considering that |
Ah I didn't even know that xp. Altough to be fair (to me), it is called |
|
I don't mind looking for a better fitting name, but I would prefer having just two functions (one for immutable, one for mutable) for fetching entities. EDIT: Well, 4 total for the panicking vs non-panicking variants. |
|
Thank you to everyone involved with the authoring or reviewing of this PR! This work is relatively important and needs release notes! Head over to bevyengine/bevy-website#1717 if you'd like to help out. |
Objective
Following the pattern established in #15593, we can reduce the API surface of
Worldby providing a single function to grab both a singular entity reference, or multiple entity references.Solution
The following functions can now also take multiple entity IDs and will return multiple entity references back:
World::entityWorld::get_entityWorld::entity_mutWorld::get_entity_mutDeferredWorld::entity_mutDeferredWorld::get_entity_mutIf you pass in X, you receive Y:
Entity, receive a singleEntityRef/EntityWorldMut(matches current behavior)[Entity; N]/&[Entity; N](array), receive an equally-sized[EntityRef; N]/[EntityMut; N]&[Entity](slice), receive aVec<EntityRef>/Vec<EntityMut>&EntityHashSet, receive aEntityHashMap<EntityRef>/EntityHashMap<EntityMut>Note that
EntityWorldMutis only returned in the single-entity case, because having multiple at the same time would lead to UB. Also,DeferredWorldreceives anEntityMutin the single-entity case because it does not allow structural access.Testing
World::entity,World::entity_mut, andDeferredWorld::entity_mutShowcase
Click to view showcase
The APIs for fetching
EntityRefs andEntityMuts from theWorldhave been unified.Querying for a single entity remains mostly the same:
Querying for multiple entities with an array has changed:
Querying for multiple entities with a slice has changed:
Querying for multiple entities with an
EntityHashSethas changed:Migration Guide
World::get_entitynow returnsResult<_, Entity>instead ofOption<_>.world.get_entity(..).ok()to return to the previous behavior.World::get_entity_mutandDeferredWorld::get_entity_mutnow returnResult<_, EntityFetchError>instead ofOption<_>.world.get_entity_mut(..).ok()to return to the previous behavior.World::entity,World::entity_mut,World::get_entity,World::get_entity_mut,DeferredWorld::entity_mut, andDeferredWorld::get_entity_muthas changed, and might now require the input argument's type to be explicitly written when inside closures.World::many_entities->World::entity::<[Entity; N]>World::many_entities_mut->World::entity_mut::<[Entity; N]>World::get_many_entities->World::get_entity::<[Entity; N]>World::get_many_entities_dynamic->World::get_entity::<&[Entity]>World::get_many_entities_mut->World::get_entity_mut::<[Entity; N]>Result<_, QueryEntityError>toResult<_, EntityFetchError>World::get_many_entities_dynamic_mut->World::get_entity_mut::<&[Entity]>Result<_, QueryEntityError>toResult<_, EntityFetchError>World::get_many_entities_from_set_mut->World::get_entity_mut::<&EntityHashSet>Result<Vec<EntityMut>, QueryEntityError>toResult<EntityHashMap<EntityMut>, EntityFetchError>. If necessary, you can still convert theEntityHashMapinto aVec.