Skip to content

Ordered iteration over Queries #1470

Closed
@alice-i-cecile

Description

@alice-i-cecile

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

Iterating over queries in a particular order is a reasonably common pattern: it is used for a) prioritization and early stopping b) ensuring reproducible execution, particularly when dealing with PRNG or modifying a common piece of data with floating point math.

It can be done manually with the current tools, but they are frustrating to write and read, and hard to optimize.

What solution would you like?

Provide a pair of Query methods: ordered_iter and ordered_iter_mut, which iterate over the entities in the query in the specified order.

In the example demonstrating this feature, show how to iterate in Entity order, and explain why using Entity as your ordering is / isn't stable, because it's the obvious thing to try and that property relies on deep engine internals.

In the example demonstrating this feature, show how to sort your entities by the value of one of their components, then iterate over the query in that order.

What alternative(s) have you considered?

Manually implement this functionality in each system using a for loop over the ordering, then using the appropriate query.get call.

Manually implement this functionality as a method on Query using a trait in each app that needs this functionality.

I'm not a fan of either of these approaches, as a) they create a lot of conceptual overhead and b) getting an optimized iteration method is challenging from the outside.

Additional context

The ordered_iter crate has a nice reference implementation / API that we can probably borrow from.

This may be efficient to implement at the same time as #762.

It's not immediately clear how / if to combine this with parallel iteration. It's useful in some cases, but nonsensical in others.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-UsabilityA targeted quality-of-life change that makes Bevy easier to use

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions