Crates.io | bevy_consumable_event |
lib.rs | bevy_consumable_event |
version | |
source | src |
created_at | 2024-01-02 09:20:20.067112 |
updated_at | 2024-11-30 08:11:08.40076 |
description | A crate to add events to Bevy that can be consumed. |
homepage | |
repository | https://github.com/andriyDev/bevy_consumable_event |
max_upload_size | |
id | 1086055 |
Cargo.toml error: | TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
A crate to add events to Bevy that can be consumed.
Events in Bevy are very powerful. However one limitation they have is that they cannot be consumed. For example, if you are clicking in a UI, you'd ideally only want one system to handle that click. Otherwise, clicking on one UI element would also click everything underneath it, making "order" irrelevant.
The canonical usage is by using App::add_consumable_event
or
App::add_persistent_consumable_event
(through the ConsumableEventApp
extension trait). Both of these functions allow you to use consumable events,
but which one determines when you can read and consume events.
App::add_consumable_event
: Events will be cleared at the start of each
frame. Therefore, only systems running in the same frame and after a
ConsumableEventWriter
will be able to read events.App::add_persistent_consumable_event
: Events are not automatically cleared
(except already consumed events). Therefore, events will stay until a system
consumes them (or you manually clear the events). Note this means that the same
system can read the same event multiple times (so it has multiple opportunities
to consume the event).In order to consume an event, simply call consume()
on the items read from the
ConsumableEventReader
.
use bevy::prelude::Event;
use bevy_consumable_event::ConsumableEventReader;
#[derive(Event)]
struct MyEvent;
fn consume_all_events(mut events: ConsumableEventReader<MyEvent>) {
for mut event in events.read() {
event.consume();
}
}
In addition, you can mutate events read from the ConsumableEventReader
(so
later reads can act on these mutations).
While using App::add_consumable_event
and
App::add_persistent_consumable_event
is the canonical way to use consumable
events, just as with regular events, these are just conveniences. You can just
as easily interact directly with ConsumableEvents
and have custom clearing
strategies using ConsumableEvents::clear
and
ConsumableEvents::clear_consumed
.
use bevy::{app::{ScheduleRunnerPlugin, RunMode}, prelude::*};
use bevy_consumable_event::{
ConsumableEventApp,
ConsumableEventReader,
ConsumableEventWriter,
};
fn main() {
App::new()
.add_plugins(ScheduleRunnerPlugin { run_mode: RunMode::Once })
.add_consumable_event::<MyEvent>()
.add_systems(Main, (
write_events,
consume_odds_and_add_ten_to_evens,
assert_remaining_events,
).chain()
)
.run();
}
#[derive(Event)]
struct MyEvent {
value: usize,
}
fn write_events(mut events: ConsumableEventWriter<MyEvent>) {
for value in 0..10 {
events.send(MyEvent { value });
}
}
fn consume_odds_and_add_ten_to_evens(
mut events: ConsumableEventReader<MyEvent>,
) {
for mut event in events.read() {
if event.value % 2 == 1 {
event.consume();
} else {
event.value += 10;
}
}
}
fn assert_remaining_events(mut events: ConsumableEventReader<MyEvent>) {
let values = events.read().map(|event| event.value).collect::<Vec<_>>();
assert_eq!(values, [10, 12, 14, 16, 18]);
}
Events and consumable events are not exclusive - it is possible to add a type as a regular event and a consumable event. Consumable events don't care interact at all with regular events, so everything will work fine. It just may be confusing.
Events also have multiple strategies to 1) prevent systems from double-reading events, and 2) prevent systems from missing events. For consumable events, the hope is these solutions aren't necessary. If you are using non-persistent consumable events, then a double read shouldn't occur (except from FixedUpdate systems). Regardless, these are just extra opportunities to consume the events. Missing events is fine since the assumption going in is that the events are consumed as they head towards the end of the frame. If you are using persistent consumable events, then a double read is expected. Missing reads are also not a problem, since events are not cleared.
License under either of
at your option.
Contributions are welcome!
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.