@@ -81,6 +81,7 @@ impl SystemMeta {
81
81
/// ```rust
82
82
/// use bevy_ecs::prelude::*;
83
83
/// use bevy_ecs::{system::SystemState};
84
+ /// use bevy_ecs::event::Events;
84
85
///
85
86
/// struct MyEvent;
86
87
/// struct MyResource(u32);
@@ -90,6 +91,7 @@ impl SystemMeta {
90
91
///
91
92
/// // Work directly on the `World`
92
93
/// let mut world = World::new();
94
+ /// world.init_resource::<Events<MyEvent>>();
93
95
///
94
96
/// // Construct a `SystemState` struct, passing in a tuple of `SystemParam`
95
97
/// // as if you were writing an ordinary system.
@@ -107,22 +109,29 @@ impl SystemMeta {
107
109
/// ```rust
108
110
/// use bevy_ecs::prelude::*;
109
111
/// use bevy_ecs::{system::SystemState};
112
+ /// use bevy_ecs::event::Events;
110
113
///
111
114
/// struct MyEvent;
115
+ /// struct CachedSystemState<'w, 's>{
116
+ /// event_state: SystemState<EventReader<'w, 's, MyEvent>>
117
+ /// }
112
118
///
113
119
/// // Create and store a system state once
114
120
/// 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);
121
+ /// world.init_resource::<Events<MyEvent>>();
122
+ /// let initial_state: SystemState<EventReader<MyEvent>> = SystemState::new(&mut world);
123
+ ///
124
+ /// // The system state is cached in a resource
125
+ /// world.insert_resource(CachedSystemState{event_state: initial_state});
118
126
///
119
127
/// // 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);
128
+ /// world.resource_scope(|world, mut cached_state: Mut<CachedSystemState>| {
129
+ /// let mut event_reader = cached_state.event_state. get_mut(world);
122
130
///
123
- /// for events in event_reader.iter(){
124
- /// // Handle events!
125
- /// };
131
+ /// for events in event_reader.iter(){
132
+ /// println!("Hello World!");
133
+ /// };
134
+ /// });
126
135
/// ```
127
136
pub struct SystemState < Param : SystemParam > {
128
137
meta : SystemMeta ,
0 commit comments