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
I'm working on a simple input system for my game and I need to dispatch the input events to several parts of my code. They all need to see the exact same events. I don't really want to collect the events into a vector because that would add an unnecessary allocation and I should be able to simply read them from Events<E> (where E is any meaningful event).
The problem is that both ManualEventReader<E> and EventReader<E> keep track the events that they dispatched. I don't need (want!) this behavior. What I wanted to do is creating a custom event reader myself that would basically be an EventReader<E> which would not mutate its internal counter.
However, this does not seem possible because most of the implementation of EventReader<E> is private and relies on private fields of both EventReader<E> and Events<E>.
Here is what I wanted to do:
#[derive(SystemParams)]structCustomEventReader<'a,E>{event_count_at_start_of_update:Local<'a,usize>,events:Res<Events<'a,E>>,}impl<E:Component>CustomEventReader<'a,E>{fniter(&self) -> implDoubleEndedIterator<Item = &'aE>{/* ??? does not modifies `event_count_at_start_of_update` */}}/// Handles events of type `E`.traitHandleEvents<E:Component>{fnhandle_events(&mutself,events:&CustomEventReader<E>){// Do things with the event reader// It is ok not to see all events. I just want to have access to all the events that were received during the last frame.for event in events.iter(){/* ... */}}}fndispatch_events_system<E:Component>(handler:ResMut<Vec<dynHandleEvents<E>>>,events:CustomEventReader<E>){for handler in&mut*handlers {// I want all the handlers to see the same events.
handler.handle_events(events);}}
Is there a way to do something like this? If not, would it be possible to add that to the api (a way to retrieve some of those private fields). I don't think this would involve any breaking changes (?)
Or maybe there's a pattern I'm not aware of?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm working on a simple input system for my game and I need to dispatch the input events to several parts of my code. They all need to see the exact same events. I don't really want to collect the events into a vector because that would add an unnecessary allocation and I should be able to simply read them from
Events<E>
(whereE
is any meaningful event).The problem is that both
ManualEventReader<E>
andEventReader<E>
keep track the events that they dispatched. I don't need (want!) this behavior. What I wanted to do is creating a custom event reader myself that would basically be anEventReader<E>
which would not mutate its internal counter.However, this does not seem possible because most of the implementation of
EventReader<E>
is private and relies on private fields of bothEventReader<E>
andEvents<E>
.Here is what I wanted to do:
Is there a way to do something like this? If not, would it be possible to add that to the api (a way to retrieve some of those private fields). I don't think this would involve any breaking changes (?)
Or maybe there's a pattern I'm not aware of?
Beta Was this translation helpful? Give feedback.
All reactions