Make event example use a local resource (#754)

Make EventListenerState a local resource
This commit is contained in:
Alec Deason 2020-10-31 00:12:56 -07:00 committed by GitHub
parent e02bd9fde1
commit ef86ce98ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,7 +7,6 @@ fn main() {
.add_default_plugins()
.add_event::<MyEvent>()
.init_resource::<EventTriggerState>()
.init_resource::<EventListenerState>()
.add_system(event_trigger_system.system())
.add_system(event_listener_system.system())
.run();
@ -43,14 +42,12 @@ fn event_trigger_system(
}
}
#[derive(Default)]
struct EventListenerState {
my_event_reader: EventReader<MyEvent>,
}
// prints events as they come in
fn event_listener_system(mut state: ResMut<EventListenerState>, my_events: Res<Events<MyEvent>>) {
for my_event in state.my_event_reader.iter(&my_events) {
fn event_listener_system(
mut my_event_reader: Local<EventReader<MyEvent>>,
my_events: Res<Events<MyEvent>>,
) {
for my_event in my_event_reader.iter(&my_events) {
println!("{}", my_event.message);
}
}