You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: crates/bevy_ecs/src/system/function_system.rs
+37-5Lines changed: 37 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -65,10 +65,22 @@ impl SystemMeta {
65
65
///
66
66
/// For an alternative approach to split mutable access to the world, see [`World::resource_scope`].
67
67
///
68
+
/// # Warning
69
+
/// This API has more performance overhead than more direct methods: prefer them when possible.
70
+
///
71
+
/// [`SystemState`] values created can be cached to improve performance,
72
+
/// and *must* be cached and reused in order for system parameters that rely on local state to work correctly.
73
+
/// These include:
74
+
/// - [`Added`](crate::system::query::Added) and [`Changed`](crate::system::query::Changed) query filters
75
+
/// - [`Local`](crate::system::Local) variables that hold state
76
+
/// - [`EventReader`](crate::event::EventReader) system parameters, which rely on a [`Local`](crate::system::Local) to track which events have been seen
77
+
///
68
78
/// # Example
79
+
///
80
+
/// Basic usage:
69
81
/// ```rust
70
-
/// use bevy::ecs::system::SystemState;
71
-
/// use bevy::ecs::world::World;
82
+
/// use bevy_ecs::prelude::*;
83
+
/// use bevy_ecs::{system::SystemState};
72
84
///
73
85
/// struct MyEvent;
74
86
/// struct MyResource(u32);
@@ -81,16 +93,36 @@ impl SystemMeta {
81
93
///
82
94
/// // Construct a `SystemState` struct, passing in a tuple of `SystemParam`
83
95
/// // as if you were writing an ordinary system.
84
-
/// // This can be cached and reused for improved performance.
85
96
/// let mut system_state: SystemState<(
86
97
/// EventWriter<MyEvent>,
87
98
/// Option<ResMut<MyResource>>,
88
99
/// Query<&MyComponent>,
89
100
/// )> = SystemState::new(&mut world);
90
101
///
91
102
/// // Use system_state.get_mut(&mut world) and unpack your system parameters into variables!
92
-
/// // You can use system_state.get(&world) instead for read-only versions of your system parameters
93
-
/// let (event_writer, maybe_respource, query) = system_state.get_mut(&mut world);
103
+
/// // system_state.get(&world) provides read-only versions of your system parameters instead.
104
+
/// let (event_writer, maybe_resource, query) = system_state.get_mut(&mut world);
105
+
/// ```
106
+
/// Caching:
107
+
/// ```rust
108
+
/// use bevy_ecs::prelude::*;
109
+
/// use bevy_ecs::{system::SystemState};
110
+
///
111
+
/// struct MyEvent;
112
+
///
113
+
/// // Create and store a system state once
114
+
/// let mut world = World::new();
115
+
/// let initial_state: SystemState<EventReader<MyEvent>> = SystemState::new(&mut world);
116
+
/// // The system state is cached directly as a resource
117
+
/// world.insert_resource(initial_state);
118
+
///
119
+
/// // Later, fetch the cached system state, saving on overhead
120
+
/// let cached_state = world.get_resource_mut::<SystemState<EventReader<MyEvent>>>().unwrap();
121
+
/// let mut event_reader = cached_state.get_mut(&mut world);
0 commit comments