Skip to content

Commit

Permalink
add example of creating systems via closures
Browse files Browse the repository at this point in the history
  • Loading branch information
cab committed May 21, 2021
1 parent 653c103 commit bfe9413
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ 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
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() {
let arg = "hello";
App::build()
.add_system(Box::new(move |mut cmd: Commands| {
println!("this system uses the moved arg: {:?}", arg);
let id = cmd.spawn().id();
println!("also it spawned an entity {:?}", id);
}).system())
.insert_resource(ExampleResource(123))
.add_startup_system(create_system(456))
.add_system(normal_system.system())
.run();
}

fn normal_system(resource: Res<ExampleResource>) {
println!("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>| {
println!("system running with arg {}", arg);
resource.0 = arg;
}).system()
}

0 comments on commit bfe9413

Please sign in to comment.