Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore separate methods for World::get_many_entities #18234

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

chescock
Copy link
Contributor

Objective

Reduce the complexity of the World::get_entity API by removing the F: WorldEntityFetch type parameter and restoring separate World::get_many_entities methods.

It would be good for consistency to have Query::get and World::get_entity work the same way, but per discussion on #18036 (comment), WorldEntityFetch was too much complexity for Query::get. Instead, let's remove WorldEntityFetch and make World::get_entity work like Query::get.

This undoes #15614 (although I did not attempt to implement it as a revert). A few things have changed since then that makes the duplication less of a problem than it was when WorldEntityFetch was introduced:

One change is that Bevy has improved handling for Results, so we no longer expect to offer panicking versions of all APIs, and we do not need to restore the panicking many_entities and many_entities_mut.

Another change is the introduction of EntitySet and related traits in #16547. This lets us generalize over types that hold sets of unique Entitys, so we do not need a separate method for every such type.

Solution

Remove F: WorldEntityFetch parameter from World::get_entity() and related methods, and have it only accept Entity.

Introduce new methods to handle the various "many" cases:

  • get_many_entities takes an array and returns an array of EntityRef. These are read-only, so duplicates are okay.
  • iter_entities takes an iterator and returns an iterator of EntityRef. These are read-only, so duplicates are okay.
  • get_many_entities_mut takes an array and returns an array of EntityMut. It performs the O(N^2) duplicate check. This is acceptable because we expect N to be small for arrays.
  • get_many_entities_unique_mut takes a UniqueEntityArray and returns an array of EntityMut. UniqueEntityArray ensures there are no duplicates.
  • iter_entities_mut takes an EntitySet and returns an iterator of EntityMut. EntitySet ensures there are no duplicates.

Also rename the existing iter_entities(_mut) methods to iter_all_entities(_mut), to free up the name.

Migration Guide

World::entity(), World::entity_mut(), World::get_entity(), and World::get_entity_mut() now only take a single Entity instead of being generic and accepting collections. If you were passing a collection, you will need to call a different method.

If you were using a panicking variant, first replace it with get_entity(e).unwrap() or get_entity_mut(e).unwrap().

Then, replace:

  • World::get_entity::<[Entity; N]> -> World::get_many_entities()
  • World::get_entity_mut::<[Entity; N]> -> World::get_many_entities_mut()
  • World::get_entity::<&[Entity]> -> World::iter_entities().collect::<Vec<_>>()
  • World::get_entity_mut::<&[Entity]> -> This has no direct equivalent, as it performed an O(N^2) check that can be expensive for large slices. First convert the collection to an EntitySet, such as EntityHashSet, then call World::iter_entities(e).
  • World::get_entity::<&EntityHashSet> -> World::iter_entities(e).map(|e| (e.id(), e)).collect::<EntityHashMap<_>>()
  • World::get_entity_mut::<&EntityHashSet> -> World::iter_entities_mut(e).map(|e| (e.id(), e)).collect::<EntityHashMap<_>>()

In addition, World::iter_entities(_mut) has been renamed iter_all_entities(_mut) to make the name available for these new methods. If you were calling iter_entities or iter_entities_mut, the functionality is still available under the new name.

chescock added 2 commits March 9, 2025 09:54
Add `get_many_entities` and `iter_entities` methods to handle lookups of multiple entities, instead.
@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Mar 10, 2025
@chescock chescock requested review from ItsDoot and Victoronz March 10, 2025 17:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Needs-Review Needs reviewer attention (from anyone!) to move forward
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants