Skip to content

Commit

Permalink
adds example for local defaults (#17751)
Browse files Browse the repository at this point in the history
# Objective
Solves #17747.

## Solution

- Adds an example for creating a default value for Local.

## Testing

- Example code compiles and passes assertions.
  • Loading branch information
newclarityex authored Feb 9, 2025
1 parent 1b7db89 commit c679b86
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions crates/bevy_ecs/src/system/system_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
///
Expand Down

0 comments on commit c679b86

Please sign in to comment.