Skip to content

implement randomize for QueryIter #14407

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

Conversation

Victoronz
Copy link
Contributor

@Victoronz Victoronz commented Jul 20, 2024

Objective

There is no out of the box way to randomize query iteration order. While order is currently not guaranteed, it is still deterministic.

Closes #14393

Solution

Implement a randomize method for QueryIter. This uses a cached sort of Entity with its hash as the key.
Since the underlying implementation is just a sort, it returns a type alias of QuerySortedIter, named QueryRandomIter.
The same could be done for QueryManyIter if #13443 merges.

Testing

The example acts as a doc test.

Showcase

Fetch 7 random query items!
let random_items = query.iter().randomize().take(7);

@Victoronz Victoronz added C-Feature A new feature, making something new possible A-ECS Entities, components, systems, and events D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jul 20, 2024
@alice-i-cecile alice-i-cecile added the X-Contentious There are nontrivial implications that should be thought through label Jul 20, 2024
@alice-i-cecile
Copy link
Member

alice-i-cecile commented Jul 20, 2024

While I really like randomizing query orders for gameplay reasons, I don't think we should add this: randomization without a seed is a serious source of tech debt. See #2504 for some previous discussion.

We could provide a better API by passing in an RNG, but that adds a dependency for pretty niche functionality. Ultimately I think the linked issue is better closed with an example, unless we want to add a full entropy / RNG solution.

@Victoronz
Copy link
Contributor Author

Victoronz commented Jul 20, 2024

makes sense, this method is a simple convenience, and if there are serious implications to adding this to bevy itself, then it is better to have the implementation fall on the user.
Maybe it makes sense to have a query_sorts example file to show this and some other applications?

@alice-i-cecile
Copy link
Member

I would call it sorting_queries, but yeah :)

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-Feature A new feature, making something new possible D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward X-Contentious There are nontrivial implications that should be thought through
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Method to get a random entity from a query
2 participants