From c679b861d82e0a0f9c22f9c7e40fd7645e28e3df Mon Sep 17 00:00:00 2001 From: newclarityex <69740123+newclarityex@users.noreply.github.com> Date: Sun, 9 Feb 2025 17:02:35 -0500 Subject: [PATCH] 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. --- crates/bevy_ecs/src/system/system_param.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/crates/bevy_ecs/src/system/system_param.rs b/crates/bevy_ecs/src/system/system_param.rs index b892748b52de5..3a464e82a1895 100644 --- a/crates/bevy_ecs/src/system/system_param.rs +++ b/crates/bevy_ecs/src/system/system_param.rs @@ -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>) -> 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: ///