From bfe941365136afe905cd071c347121ee0b6afefb Mon Sep 17 00:00:00 2001 From: conner bryan Date: Fri, 21 May 2021 07:52:26 -0400 Subject: [PATCH] add example of creating systems via closures --- Cargo.toml | 5 +++++ examples/ecs/system_closures.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 examples/ecs/system_closures.rs diff --git a/Cargo.toml b/Cargo.toml index 898ab0bbc3b8fd..01287787f6cacc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/examples/ecs/system_closures.rs b/examples/ecs/system_closures.rs new file mode 100644 index 00000000000000..e0d59cb5734ec1 --- /dev/null +++ b/examples/ecs/system_closures.rs @@ -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) { + 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 { + Box::new(move |mut resource: ResMut| { + println!("system running with arg {}", arg); + resource.0 = arg; + }).system() +} \ No newline at end of file