adds example for local defaults (#17751)
# Objective Solves https://github.com/bevyengine/bevy/issues/17747. ## Solution - Adds an example for creating a default value for Local. ## Testing - Example code compiles and passes assertions.
This commit is contained in:
parent
1b7db895b7
commit
c679b861d8
@ -1129,6 +1129,25 @@ unsafe impl<'w> SystemParam for DeferredWorld<'w> {
|
||||
/// assert_eq!(read_system.run((), world), 0);
|
||||
/// ```
|
||||
///
|
||||
/// A simple way to set a different default value for a local is by wrapping the value with an Option.
|
||||
///
|
||||
/// ```
|
||||
/// # use bevy_ecs::prelude::*;
|
||||
/// # let world = &mut World::default();
|
||||
/// fn counter_from_10(mut count: Local<Option<usize>>) -> usize {
|
||||
/// let count = count.get_or_insert(10);
|
||||
/// *count += 1;
|
||||
/// *count
|
||||
/// }
|
||||
/// let mut counter_system = IntoSystem::into_system(counter_from_10);
|
||||
/// counter_system.initialize(world);
|
||||
///
|
||||
/// // Counter is initialized at 10, and increases to 11 on first run.
|
||||
/// assert_eq!(counter_system.run((), world), 11);
|
||||
/// // Counter is only increased by 1 on subsequent runs.
|
||||
/// assert_eq!(counter_system.run((), world), 12);
|
||||
/// ```
|
||||
///
|
||||
/// N.B. A [`Local`]s value cannot be read or written to outside of the containing system.
|
||||
/// To add configuration to a system, convert a capturing closure into the system instead:
|
||||
///
|
||||
|
Loading…
Reference in New Issue
Block a user