is this valid
#[derive(Resource)]
pub struct ApplyInstantEffectsEventReaderState {
event_state: SystemState<EventReader<'static, 'static, ApplyInstantEffectEvent>>,
}
fn init_apply_instant_effects_reader_state(
world: &mut World,
){
// Create and store a system state once
let mut world = World::new();
world.init_resource::<Events<ApplyInstantEffectEvent>>();
let initial_state: SystemState<EventReader<ApplyInstantEffectEvent>> = SystemState::new(&mut world);
// The system state is cached in a resource
world.insert_resource(ApplyInstantEffectsEventReaderState {
event_state: initial_state,
});
}
fn apply_instant_effects(
world: &mut World,
mut apply_instant_effects_reader_state: ResMut<ApplyInstantEffectsEventReaderState>
) {
// Step 1: Collect all events first
let mut effects_to_apply = Vec::new();
{
// Create a scope to limit the mutable borrow of `world`
let mut event_reader = apply_instant_effects_reader_state.event_state.get_mut(world);
for event in event_reader.read() {
effects_to_apply.push(event.clone());
}
} // `event_reader` goes out of scope here, releasing the mutable borrow on `world`
for evt in effects_to_apply {
let effect_application = evt.effect_application.clone();
let source = evt.source_entity;
let target = evt.target_entity;
let contact_position = evt.contact_position;
effect_application.apply_to_world(source, target, contact_position, &mut world);
}
}