Description
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.