Struct Local
pub struct Local<'s, T>(/* private fields */)
where
T: FromWorld + Send + 'static;Expand description
A [SystemParam] that provides a system-private value of T that persists across system calls.
The initial value is created by calling T’s FromWorld::from_world (or Default::default if T: Default).
A local may only be accessed by the system itself and is therefore not visible to other systems.
If two or more systems specify the same local type each will have their own unique local.
If multiple [SystemParam]s within the same system each specify the same local type
each will get their own distinct data storage.
The supplied lifetime parameter is the [SystemParam]s 's lifetime.
§Examples
fn counter(mut count: Local<u32>) -> u32 {
*count += 1;
*count
}
let mut counter_system = IntoSystem::into_system(counter);
counter_system.initialize(world);
// Counter is initialized to u32's default value of 0, and increases to 1 on first run.
assert_eq!(counter_system.run((), world).unwrap(), 1);
// Counter gets the same value and increases to 2 on its second call.
assert_eq!(counter_system.run((), world).unwrap(), 2);A simple way to set a different default value for a local is by wrapping the value with an Option.
fn counter_from_10(mut count: Local<Option<u32>>) -> u32 {
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).unwrap(), 11);
// Counter is only increased by 1 on subsequent runs.
assert_eq!(counter_system.run((), world).unwrap(), 12);A system can have multiple Local values with the same type, each with distinct values.
fn double_counter(mut count: Local<u32>, mut double_count: Local<u32>) -> (u32, u32) {
*count += 1;
*double_count += 2;
(*count, *double_count)
}
let mut counter_system = IntoSystem::into_system(double_counter);
counter_system.initialize(world);
assert_eq!(counter_system.run((), world).unwrap(), (1, 2));
assert_eq!(counter_system.run((), world).unwrap(), (2, 4));This example shows that two systems using the same type for their own Local get distinct locals.
fn write_to_local(mut local: Local<usize>) {
*local = 42;
}
fn read_from_local(local: Local<usize>) -> usize {
*local
}
let mut write_system = IntoSystem::into_system(write_to_local);
let mut read_system = IntoSystem::into_system(read_from_local);
write_system.initialize(world);
read_system.initialize(world);
assert_eq!(read_system.run((), world).unwrap(), 0);
write_system.run((), world);
// The read local is still 0 due to the locals not being shared.
assert_eq!(read_system.run((), world).unwrap(), 0);You can use a Local to avoid reallocating memory every system call.
fn some_system(mut vec: Local<Vec<u32>>) {
// Do your regular system logic, using the vec, as normal.
// At end of function, clear the vec's contents so its empty for next system call.
// If it's possible the capacity could get too large, you may want to check and resize that as well.
vec.clear();
}N.B. A Locals 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:
struct Config(u32);
#[derive(Resource)]
struct MyU32Wrapper(u32);
fn reset_to_system(value: Config) -> impl FnMut(ResMut<MyU32Wrapper>) {
move |mut val| val.0 = value.0
}
// .add_systems(reset_to_system(my_config))Trait Implementations§
§impl<'_s, T> ExclusiveSystemParam for Local<'_s, T>
impl<'_s, T> ExclusiveSystemParam for Local<'_s, T>
§impl<'s, 'a, T> IntoIterator for &'a Local<'s, T>
impl<'s, 'a, T> IntoIterator for &'a Local<'s, T>
§impl<'s, 'a, T> IntoIterator for &'a mut Local<'s, T>
impl<'s, 'a, T> IntoIterator for &'a mut Local<'s, T>
§type Item = <&'a mut T as IntoIterator>::Item
type Item = <&'a mut T as IntoIterator>::Item
§type IntoIter = <&'a mut T as IntoIterator>::IntoIter
type IntoIter = <&'a mut T as IntoIterator>::IntoIter
§impl<'a, T> SystemParam for Local<'a, T>
impl<'a, T> SystemParam for Local<'a, T>
§type Item<'w, 's> = Local<'s, T>
type Item<'w, 's> = Local<'s, T>
Self, instantiated with new lifetimes. Read more§fn init_state(world: &mut World) -> <Local<'a, T> as SystemParam>::State
fn init_state(world: &mut World) -> <Local<'a, T> as SystemParam>::State
State.§fn init_access(
_state: &<Local<'a, T> as SystemParam>::State,
_system_meta: &mut SystemMeta,
_component_access_set: &mut FilteredAccessSet,
_world: &mut World,
)
fn init_access( _state: &<Local<'a, T> as SystemParam>::State, _system_meta: &mut SystemMeta, _component_access_set: &mut FilteredAccessSet, _world: &mut World, )
§unsafe fn get_param<'w, 's>(
state: &'s mut <Local<'a, T> as SystemParam>::State,
_system_meta: &SystemMeta,
_world: UnsafeWorldCell<'w>,
_change_tick: Tick,
) -> Result<<Local<'a, T> as SystemParam>::Item<'w, 's>, SystemParamValidationError>
unsafe fn get_param<'w, 's>( state: &'s mut <Local<'a, T> as SystemParam>::State, _system_meta: &SystemMeta, _world: UnsafeWorldCell<'w>, _change_tick: Tick, ) -> Result<<Local<'a, T> as SystemParam>::Item<'w, 's>, SystemParamValidationError>
SystemParamFunction. Read more§fn apply(state: &mut Self::State, system_meta: &SystemMeta, world: &mut World)
fn apply(state: &mut Self::State, system_meta: &SystemMeta, world: &mut World)
SystemParam]’s state.
This is used to apply Commands during ApplyDeferred.§fn queue(
state: &mut Self::State,
system_meta: &SystemMeta,
world: DeferredWorld<'_>,
)
fn queue( state: &mut Self::State, system_meta: &SystemMeta, world: DeferredWorld<'_>, )
ApplyDeferred.§impl<'s, T> SystemParamBuilder<Local<'s, T>> for LocalBuilder<T>
impl<'s, T> SystemParamBuilder<Local<'s, T>> for LocalBuilder<T>
§fn build_state(self, world: &mut World) -> SystemState<P>
fn build_state(self, world: &mut World) -> SystemState<P>
SystemState] from a SystemParamBuilder.
To create a system, call [SystemState::build_system] on the result.