Skip to content

Select a random element (or set of elements) from a query #3652

@alice-i-cecile

Description

@alice-i-cecile

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

Selecting random elements is a common gameplay tasks. We can quickly and ergonomically generate possible lists of

What solution would you like?

Implement the SliceRandom trait for Query.
Nope, this is not publically implementable. We have to use the alternative, implementing it for our associated iterator type.

Create an example.

What alternative(s) have you considered?

  1. Users could manually implement this functionality each time. This is tedious, error prone and probably slower.
  2. We could create our own methods that do this. This breaks from the ecosystem standard for no good reason.
  3. We could implement the IteratorRandom trait instead. Taking a look, our ability to mutate the elements of the query + desire to be able to shuffle the order probably makes SliceRandom more fitting. More importantly, Query isn't itself an iterator (nor should it be): being able to operate directly on a query rather than query.iter() is more natural and convenient.

Additional context

Related to #1470.

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