Skip to content

Query::get_multiple for accessing multiple entities simultaneously from the same query #2276

Description

@alice-i-cecile

What problem does this solve or what need does it fill?

When working with queries, you sometimes need to access the data on multiple components at once. This is particularly frustrating if you require mutable access, due to Rust's borrow checking rules.

What solution would you like?

Add the Query::get_multiple(entities: HashSet<Entity>) -> HashMap<Entity, Result<[Elided](https://docs.rs/bevy/0.5.0/bevy/ecs/system/struct.Query.html#method.get_mut)>> method and the associated get_multiple_mut. By passing in a HashSet of entities, we can ensure that we never have mutable access to the entities at the same time.

Also add get_component analogues for this pattern.

What alternative(s) have you considered?

iter_combinations solves the common case, but other more bespoke access patterns certainly exist.

You can work around this by storing data, but that only works for Clone components.

@TheRawMeatball felt this should use a different API / impl:

it'd be more like a wrapper you put the query into
and then it'd pass out smart pointers wrappers of smart pointers

Additional context

#1470 may be easier to work around with this style of API.

It may make sense to use this implementation for iter_combinations in some way, but be mindful of perf as iter_combinations is likely to be used in tight loops for things like collision detection.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-FeatureA new feature, making something new possibleS-Ready-For-ImplementationThis issue is ready for an implementation PR. Go for it!

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions