|
| 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