This repository was archived by the owner on Jan 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Event Bridge Pattern #6
Draft
Pfeil
wants to merge
1
commit into
MiniaczQ:0.14
Choose a base branch
from
Pfeil:generic-events
base: 0.14
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| [package] | ||
| name = "event-bridge" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
|
||
| [dependencies] | ||
| bevy = "0.14" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Pattern Name: Event bridge | ||
|
|
||
| ## Description | ||
|
|
||
| Send events between your plugins/systems without a shared event type (or other imports). This can make your plugins fully independent and exchangeable. The small amount of boilerplate per system will be dependent on the plugin, though. Making your plugins send/receive generic events allows a project to use an actual custom event type to be used in the ECS. This is done by translating before sending and after receiving an event. | ||
|
|
||
| ## Implementation | ||
|
|
||
| [Implementation on both sides (event emitter and receiver plugins)](./src/main.rs) | ||
|
|
||
| ## Use cases | ||
|
|
||
| This pattern is applied to plugins/systems making use of events. Such plugins can then be connected using a small amount of code within your project. | ||
|
|
||
| You can use it to connect plugins in a very loose way without any of them knowing anything about the other. It does not introduce additional game cycles for the event transfer, but add some conversion overhead (usually very small). The connecting event can be fully customized. | ||
|
|
||
| ## Alternatives | ||
|
|
||
| - Translation systems (a system function which receives all events of a given type and for each emits another type of event) | ||
| - Accept dependencies (e.g. shared types) between (internal-only) plugins | ||
|
|
||
| ## Credit | ||
|
|
||
| This makes use of [bevy's support for generics](https://bevy-cheatbook.github.io/patterns/generic-systems.html) and the [From-trait from the rust standard library](https://doc.rust-lang.org/std/convert/trait.From.html). | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| use bevy::prelude::*; | ||
| use boilerplate::*; | ||
|
|
||
| mod boilerplate { | ||
| use bevy::ecs::event::Event; | ||
|
|
||
| use super::event_actor::ActorEvent; | ||
| use super::event_emitter::EmitterEvent; | ||
|
|
||
| #[derive(Event, Clone)] | ||
| /// Glue for the different event types of the plugins | ||
| pub struct GameEvent(pub &'static str); | ||
|
|
||
| impl From<EmitterEvent> for GameEvent { | ||
| fn from(other: EmitterEvent) -> Self { | ||
| Self(other.0) | ||
| } | ||
| } | ||
|
|
||
| impl From<GameEvent> for ActorEvent { | ||
| fn from(other: GameEvent) -> Self { | ||
| Self(other.0.len()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| App::new() | ||
| .add_event::<GameEvent>() | ||
| .add_plugins(DefaultPlugins) | ||
|
|
||
| // Note you can comment out each plugin without breaking code | ||
| .add_plugins(event_emitter::plugin::<GameEvent>) | ||
| .add_plugins(event_actor::plugin::<GameEvent>) | ||
|
|
||
| .run(); | ||
| } | ||
|
|
||
| mod event_actor { | ||
| // Note there is no import from the emitter | ||
| use bevy::ecs::event::Event; | ||
| use bevy::prelude::*; | ||
|
|
||
| #[derive(Event)] | ||
| pub struct ActorEvent(pub usize); | ||
|
|
||
| pub fn plugin<E: Event + Clone + Into<ActorEvent>>(app: &mut App) { | ||
| app.add_systems(Update, act::<E>); | ||
| } | ||
|
|
||
| fn act<E: Event + Clone + Into<ActorEvent>>(mut events_in: EventReader<E>) { | ||
| for event in events_in.read() { | ||
| // with this transformation we can act on all of the data of the event. | ||
| let event: ActorEvent = (*event).clone().into(); | ||
| info!("Event: {}", event.0); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| mod event_emitter { | ||
| // Note there is no import from the actor | ||
| use bevy::ecs::event::Event; | ||
| use bevy::prelude::*; | ||
|
|
||
| #[derive(Event)] | ||
| pub struct EmitterEvent(pub &'static str); | ||
|
|
||
| pub fn plugin<E: Event + From<EmitterEvent>>(app: &mut App) { | ||
| app.add_event::<EmitterEvent>() | ||
| .add_systems(PreUpdate, send::<E>); | ||
| } | ||
|
|
||
| fn send<E: Event + From<EmitterEvent>>(mut events_out: EventWriter<E>) { | ||
| events_out.send(E::from(EmitterEvent("my event"))); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not super sold on the
From, instead of having a:we create this dependency abomination