Skip to content

Add random_query example #3704

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ path = "examples/ecs/iter_combinations.rs"
name = "parallel_query"
path = "examples/ecs/parallel_query.rs"

[[example]]
name = "random_query"
path = "examples/ecs/random_query.rs"

[[example]]
name = "removal_detection"
path = "examples/ecs/removal_detection.rs"
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ Example | File | Description
`hierarchy` | [`ecs/hierarchy.rs`](./ecs/hierarchy.rs) | Creates a hierarchy of parents and children entities
`iter_combinations` | [`ecs/iter_combinations.rs`](./ecs/iter_combinations.rs) | Shows how to iterate over combinations of query results.
`parallel_query` | [`ecs/parallel_query.rs`](./ecs/parallel_query.rs) | Illustrates parallel queries with `ParallelIterator`
`random_query` | [`ecs/random_query.rs`](./ecs/random_query.rs) | Sample random elements of a query with `rand::seq::IteratorRandom`
`removal_detection` | [`ecs/removal_detection.rs`](./ecs/removal_detection.rs) | Query for entities that had a specific component removed in a previous stage during the current frame.
`startup_system` | [`ecs/startup_system.rs`](./ecs/startup_system.rs) | Demonstrates a startup system (one that runs once when the app starts up)
`state` | [`ecs/state.rs`](./ecs/state.rs) | Illustrates how to use States to control transitioning from a Menu state to an InGame state
Expand Down
43 changes: 43 additions & 0 deletions examples/ecs/random_query.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use bevy::prelude::*;
use rand::seq::IteratorRandom;

#[derive(Component, Clone, Copy)]
struct Pet(char);

#[derive(Component, Clone)]
struct Name(String);

fn main() {
App::new()
.add_startup_system(generate_names)
.add_startup_system(generate_pets)
.add_system(random_people)
.run();
}

fn generate_names(mut commands: Commands) {
let names = ["Linus", "Noah", "Elijah", "Olivia", "Ava", "Charlotte"];
for name in names {
commands.spawn().insert(Name(name.to_owned()));
}
}

fn generate_pets(mut commands: Commands) {
let pets = ['🐀', '🐄', '🐅', '🐇', '🐊', '🐓', '🐖'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

for pet in pets {
commands.spawn().insert(Pet(pet));
}
}

fn random_people(name_query: Query<&Name>, pet_query: Query<&Pet>) {
let mut rng = rand::thread_rng();
// Use [`IteratorRandom::choose_multiple`] to pick three random names
let names: Vec<&Name> = name_query.iter().choose_multiple(&mut rng, 3);
for name in names {
// Use [`IteratorRandom::choose`] to pick one random pet
let pet: Option<&Pet> = pet_query.iter().choose(&mut rng);
// `pet` will only be `None` if the query found no pets. That shouldn't happen here.
let pet = pet.unwrap();
println!("{} owns a {}.", name.0, pet.0);
}
}