Skip to content

Commit 1083fc8

Browse files
bytemuckexjam
authored andcommitted
Added example of creating a system from a closure (bevyengine#4327)
Fixes bevyengine#4262
1 parent 85e3022 commit 1083fc8

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,10 @@ path = "examples/ecs/state.rs"
391391
name = "system_chaining"
392392
path = "examples/ecs/system_chaining.rs"
393393

394+
[[example]]
395+
name = "system_closure"
396+
path = "examples/ecs/system_closure.rs"
397+
394398
[[example]]
395399
name = "system_param"
396400
path = "examples/ecs/system_param.rs"

examples/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ Example | File | Description
190190
`startup_system` | [`ecs/startup_system.rs`](./ecs/startup_system.rs) | Demonstrates a startup system (one that runs once when the app starts up)
191191
`state` | [`ecs/state.rs`](./ecs/state.rs) | Illustrates how to use States to control transitioning from a Menu state to an InGame state
192192
`system_chaining` | [`ecs/system_chaining.rs`](./ecs/system_chaining.rs) | Chain two systems together, specifying a return type in a system (such as `Result`)
193+
`system_closure` | [`ecs/system_closure.rs`](./ecs/system_closure.rs) | Show how to use closures as systems, and how to configure `Local` variables by capturing external state
193194
`system_param` | [`ecs/system_param.rs`](./ecs/system_param.rs) | Illustrates creating custom system parameters with `SystemParam`
194195
`system_sets` | [`ecs/system_sets.rs`](./ecs/system_sets.rs) | Shows `SystemSet` use along with run criterion
195196
`timers` | [`ecs/timers.rs`](./ecs/timers.rs) | Illustrates ticking `Timer` resources inside systems and handling their state

examples/ecs/system_closure.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use bevy::{log::LogPlugin, prelude::*};
2+
3+
fn main() {
4+
// create a simple closure.
5+
let simple_closure = || {
6+
// this is a closure that does nothing.
7+
info!("Hello from a simple closure!");
8+
};
9+
10+
// create a closure, with an 'input' value.
11+
let complex_closure = |mut value: String| {
12+
move || {
13+
info!("Hello from a complex closure! {:?}", value);
14+
15+
// we can modify the value inside the closure. this will be saved between calls.
16+
value = format!("{} - updated", value);
17+
18+
// you could also use an outside variable like presented in the inlined closures
19+
// info!("outside_variable! {:?}", outside_variable);
20+
}
21+
};
22+
23+
let outside_variable = "bar".to_string();
24+
25+
App::new()
26+
.add_plugin(LogPlugin)
27+
// we can use a closure as a system
28+
.add_system(simple_closure)
29+
// or we can use a more complex closure, and pass an argument to initialize a Local variable.
30+
.add_system(complex_closure("foo".into()))
31+
// we can also inline a closure
32+
.add_system(|| {
33+
info!("Hello from an inlined closure!");
34+
})
35+
// or use variables outside a closure
36+
.add_system(move || {
37+
info!(
38+
"Hello from an inlined closure that captured the 'outside_variable'! {:?}",
39+
outside_variable
40+
);
41+
// you can use outside_variable, or any other variables inside this closure.
42+
// their states will be saved.
43+
})
44+
.run();
45+
}

0 commit comments

Comments
 (0)