Skip to content

Commit c679b86

Browse files
authored
adds example for local defaults (#17751)
# Objective Solves #17747. ## Solution - Adds an example for creating a default value for Local. ## Testing - Example code compiles and passes assertions.
1 parent 1b7db89 commit c679b86

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

crates/bevy_ecs/src/system/system_param.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,25 @@ unsafe impl<'w> SystemParam for DeferredWorld<'w> {
11291129
/// assert_eq!(read_system.run((), world), 0);
11301130
/// ```
11311131
///
1132+
/// A simple way to set a different default value for a local is by wrapping the value with an Option.
1133+
///
1134+
/// ```
1135+
/// # use bevy_ecs::prelude::*;
1136+
/// # let world = &mut World::default();
1137+
/// fn counter_from_10(mut count: Local<Option<usize>>) -> usize {
1138+
/// let count = count.get_or_insert(10);
1139+
/// *count += 1;
1140+
/// *count
1141+
/// }
1142+
/// let mut counter_system = IntoSystem::into_system(counter_from_10);
1143+
/// counter_system.initialize(world);
1144+
///
1145+
/// // Counter is initialized at 10, and increases to 11 on first run.
1146+
/// assert_eq!(counter_system.run((), world), 11);
1147+
/// // Counter is only increased by 1 on subsequent runs.
1148+
/// assert_eq!(counter_system.run((), world), 12);
1149+
/// ```
1150+
///
11321151
/// N.B. A [`Local`]s value cannot be read or written to outside of the containing system.
11331152
/// To add configuration to a system, convert a capturing closure into the system instead:
11341153
///

0 commit comments

Comments
 (0)