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:
newclarityex 2025-02-09 17:02:35 -05:00 committed by GitHub
parent 1b7db895b7
commit c679b861d8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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:
///