Skip to content
This repository was archived by the owner on Jan 3, 2025. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ members = [
"patterns/component_installer",
"patterns/deferred_spawn",
"patterns/dependency_hook",
"patterns/event_bridge",
]
9 changes: 9 additions & 0 deletions patterns/event_bridge/Cargo.toml
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"
25 changes: 25 additions & 0 deletions patterns/event_bridge/README.md
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).

76 changes: 76 additions & 0 deletions patterns/event_bridge/src/main.rs
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())
}
}
Comment on lines +14 to +24
Copy link
Copy Markdown
Owner

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:

graph TD
  base --base event--> pluginA
  base --base event--> pluginB
  pluginA --> game
  pluginB --> game
Loading

we create this dependency abomination

graph TD
  base --base event--> pluginA
  base --base event--> pluginB
  pluginA --event A--> base
  pluginB --event B--> base
  pluginA --> game
  pluginB --> game
Loading

}

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")));
}
}