Skip to content
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

Added a generic systems example to the examples folder in bevy. #2191

Closed
wants to merge 8 commits into from
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ path = "examples/ecs/timers.rs"
name = "query_bundle"
path = "examples/ecs/query_bundle.rs"

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

# Games
[[example]]
name = "alien_cake_addict"
Expand Down
26 changes: 26 additions & 0 deletions examples/ecs/generic_systems.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use bevy::prelude::*;
use bevy::ecs::component::Component;
AmrDhaliwal marked this conversation as resolved.
Show resolved Hide resolved

struct A;
struct B;

fn spawn_entities_on_click<T: Component, const N: usize>(
mut cmds: Commands,
mouse_button_inputs: Res<Input<MouseButton>>,
) {
if mouse_button_inputs.just_pressed(MouseButton::Left) {
for _ in 0..N {
cmds.spawn().insert(T).id();
}
info!("spawned {} entities with component {}", N, std::any::type_name::<T>());
}
}
AmrDhaliwal marked this conversation as resolved.
Show resolved Hide resolved


fn main() {
App::build()
.add_plugins(DefaultPlugins)
.add_system(spawn_entities_on_click::<A>.system())
.add_system(spawn_entities_on_click::<B>.system())
.run();
}
AmrDhaliwal marked this conversation as resolved.
Show resolved Hide resolved