Skip to content
This repository was archived by the owner on Jan 3, 2025. It is now read-only.

Event Bridge Pattern#6

Draft
Pfeil wants to merge 1 commit intoMiniaczQ:0.14from
Pfeil:generic-events
Draft

Event Bridge Pattern#6
Pfeil wants to merge 1 commit intoMiniaczQ:0.14from
Pfeil:generic-events

Conversation

@Pfeil
Copy link
Copy Markdown

@Pfeil Pfeil commented Jul 25, 2024

This is about a pattern to make plugins (even internal ones) independent of other code, even in terms of imports.

https://github.com/Pfeil/bevy-design-patterns/tree/generic-events/patterns/event_bridge

Let's shortly define some properties / levels of independence of a plugin:

  1. the initialization (add_plugins) can be omitted, and your code will still work without any other adjustments like removing imports.
  2. the plugin can be fully removed, and your code will still work without any other adjustments like removing imports.

I was aiming for both properties. So far, it at least works in the example case. Not sure how it applies in practice. This is a really early idea.

Things which came to my mind, but I was not able to try yet:

  • can it be applied to more than events? Like components or other types one would need to import?
  • is a configuration resource an alternative, and in which cases?
  • can we make a wrapper module which packs the boilerplate and the plugin module in order to further structure the imports in a hierarchical, useful way? (probably yes)
  • I believe this is already useful if either the sender or the emitter implement the pattern, at least in one way. I did not try yet, though. We could add some examples, e.g., by using a src/bin folder.
  • We have a clone in there, we can probably avoid it somehow.
  • I'd like to test / see it in some greater context to see how it works in practice.

@MiniaczQ MiniaczQ changed the title new pattern: event bridge Event Bridge Pattern Jul 25, 2024
@MiniaczQ
Copy link
Copy Markdown
Owner

As for generalizing the pattern, observers also work on Event types, so that's something this could tackle 🤔

Comment on lines +14 to +24
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())
}
}
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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants