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
28 changes: 28 additions & 0 deletions examples/ecs/generic_systems.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use bevy::{prelude::*, ecs::component::Component};

fn main() {
App::build()
.add_plugins(DefaultPlugins)
.add_system(spawn_entities_on_click::<A>.system())
Copy link
Member

Choose a reason for hiding this comment

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

I think this needs the second type parameter to work :)

Copy link
Author

Choose a reason for hiding this comment

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

@alice-i-cecile do you mean the second parameter type for the function?

.add_system(spawn_entities_on_click::<B>.system())
.run();
}

struct A;
struct B;

fn spawn_entities_on_click<T: Component, const N: usize>(
mut cmds: Commands,
mouse_button_inputs: Res<Input<MouseButton>>,
)
where
T: Component + Default
{
if mouse_button_inputs.just_pressed(MouseButton::Left) {
for _ in 0..N {
cmds.spawn().insert(T::default()).id();
}
info!("spawned {} entities with component {}", N, std::any::type_name::<T>());
}
}