Skip to content

Commit 2590fd0

Browse files
Basic explanation of why SystemState is useful
1 parent d8974e7 commit 2590fd0

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

crates/bevy_ecs/src/system/function_system.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,40 @@ impl SystemMeta {
5656
// TODO: Actually use this in FunctionSystem. We should probably only do this once Systems are constructed using a World reference
5757
// (to avoid the need for unwrapping to retrieve SystemMeta)
5858
/// Holds on to persistent state required to drive [`SystemParam`] for a [`System`].
59+
///
60+
/// This is a very powerful and convenient tool for working with exclusive world access,
61+
/// allowing you to fetch data from the [`World`] as if you were running a [`System`].
62+
///
63+
/// Borrow-checking is handled for you, allowing you to mutably acccess multiple compatible system parameters at once,
64+
/// and arbitrary system parameters (like [`EventWriter`](crate::event::EventWriter)) can be conveniently fetched.
65+
///
66+
/// # Example
67+
/// ```rust
68+
/// use bevy::ecs::system::SystemState;
69+
/// use bevy::ecs::world::World;
70+
///
71+
/// struct MyEvent;
72+
/// struct MyResource(u32);
73+
///
74+
/// #[derive(Component)]
75+
/// struct MyComponent;
76+
///
77+
/// // Work directly on the `World`
78+
/// let mut world = World::new();
79+
///
80+
/// // Construct a `SystemState` struct, passing in a tuple of `SystemParam`
81+
/// // as if you were writing an ordinary system.
82+
/// // This can be cached and reused for improved performance.
83+
/// let mut system_state: SystemState<(
84+
/// EventWriter<MyEvent>,
85+
/// Option<ResMut<MyResource>>,
86+
/// Query<&MyComponent>,
87+
/// )> = SystemState::new(&mut world);
88+
///
89+
/// // Use system_state.get_mut(&mut world) and unpack your system parameters into variables!
90+
/// // You can use system_state.get(&world) instead for read-only versions of your system parameters
91+
/// let (event_writer, maybe_respource, query) = system_state.get_mut(&mut world);
92+
/// ```
5993
pub struct SystemState<Param: SystemParam> {
6094
meta: SystemMeta,
6195
param_state: <Param as SystemParam>::Fetch,

0 commit comments

Comments
 (0)