bevy/examples/ecs/system_closure.rs
Edgar Geier f18f28874a
Allow tuples and single plugins in add_plugins, deprecate add_plugin (#8097)
# Objective

- Better consistency with `add_systems`.
- Deprecating `add_plugin` in favor of a more powerful `add_plugins`.
- Allow passing `Plugin` to `add_plugins`.
- Allow passing tuples to `add_plugins`.

## Solution

- `App::add_plugins` now takes an `impl Plugins` parameter.
- `App::add_plugin` is deprecated.
- `Plugins` is a new sealed trait that is only implemented for `Plugin`,
`PluginGroup` and tuples over `Plugins`.
- All examples, benchmarks and tests are changed to use `add_plugins`,
using tuples where appropriate.

---

## Changelog

### Changed

- `App::add_plugins` now accepts all types that implement `Plugins`,
which is implemented for:
  - Types that implement `Plugin`.
  - Types that implement `PluginGroup`.
  - Tuples (up to 16 elements) over types that implement `Plugins`.
- Deprecated `App::add_plugin` in favor of `App::add_plugins`.

## Migration Guide

- Replace `app.add_plugin(plugin)` calls with `app.add_plugins(plugin)`.

---------

Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2023-06-21 20:51:03 +00:00

47 lines
1.6 KiB
Rust

//! Shows how anonymous functions / closures can be used as systems.
use bevy::{log::LogPlugin, prelude::*};
fn main() {
// create a simple closure.
let simple_closure = || {
// this is a closure that does nothing.
info!("Hello from a simple closure!");
};
// create a closure, with an 'input' value.
let complex_closure = |mut value: String| {
move || {
info!("Hello from a complex closure! {:?}", value);
// we can modify the value inside the closure. this will be saved between calls.
value = format!("{value} - updated");
// you could also use an outside variable like presented in the inlined closures
// info!("outside_variable! {:?}", outside_variable);
}
};
let outside_variable = "bar".to_string();
App::new()
.add_plugins(LogPlugin::default())
// we can use a closure as a system
.add_systems(Update, simple_closure)
// or we can use a more complex closure, and pass an argument to initialize a Local variable.
.add_systems(Update, complex_closure("foo".into()))
// we can also inline a closure
.add_systems(Update, || {
info!("Hello from an inlined closure!");
})
// or use variables outside a closure
.add_systems(Update, move || {
info!(
"Hello from an inlined closure that captured the 'outside_variable'! {:?}",
outside_variable
);
// you can use outside_variable, or any other variables inside this closure.
// their states will be saved.
})
.run();
}