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

add example of creating systems via closures #2231

Closed
wants to merge 1 commit 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 @@ -287,6 +287,10 @@ path = "examples/ecs/startup_system.rs"
name = "state"
path = "examples/ecs/state.rs"

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

[[example]]
name = "system_chaining"
path = "examples/ecs/system_chaining.rs"
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ Example | File | Description
`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
`system_chaining` | [`ecs/system_chaining.rs`](./ecs/system_chaining.rs) | Chain two systems together, specifying a return type in a system (such as `Result`)
`system_closures` | [`ecs/system_closures.rs`](./ecs/system_closures.rs) | Shows how to create and execute systems from closures.
`system_param` | [`ecs/system_param.rs`](./ecs/system_param.rs) | Illustrates creating custom system parameters with `SystemParam`
`system_sets` | [`ecs/system_sets.rs`](./ecs/system_sets.rs) | Shows `SystemSet` use along with run criterion
`timers` | [`ecs/timers.rs`](./ecs/timers.rs) | Illustrates ticking `Timer` resources inside systems and handling their state
Expand Down
29 changes: 29 additions & 0 deletions examples/ecs/system_closures.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use bevy::prelude::*;

fn main() {
App::build()
.add_plugin(bevy::log::LogPlugin::default())
.add_system(Box::new(|mut cmd: Commands, arg: Local<String>| {
info!("this system uses an argument: {:?}", arg);
let id = cmd.spawn().id();
info!("also it spawned an entity {:?}", id);
}).system().config(|config| config.1 = Some("hello".to_string())))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
}).system().config(|config| config.1 = Some("hello".to_string())))
}).system())

Unless the .config() is completely necessary here, I feel like it's just adding unnecessary noise.

Copy link
Author

@cab cab May 21, 2021

Choose a reason for hiding this comment

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

I think it is necessary -- that's what sets the Local<String>.

.insert_resource(ExampleResource(123))
.add_startup_system(create_system(456))
.add_system(normal_system.system())
.run();
}

fn normal_system(resource: Res<ExampleResource>) {
info!("resource has value {:?}", resource.0);
}

pub struct ExampleResource(usize);

// Creates a system that modifies a resource.
fn create_system(arg: usize) -> impl bevy::ecs::system::System<In = (), Out = ()> {
Box::new(move |mut resource: ResMut<ExampleResource>| {
info!("system running with captured arg {}", arg);
resource.0 = arg;
}).system()
}