-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Observers that can observe multiple different event types #14649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Rather than implement a new "query style" API, we could also consider: #[derive(Event)]
struct Foo { foo: i32 }
#[derive(Event)]
struct Bar { bar: bool }
#[derive(Event)]
enum Combined {
Foo(Foo),
Bar(Bar),
}
// Pretty sure this type elision works out
world
.observe(trigger_map(Combined::Foo))
.observe(trigger_map(Combined::Bar))
world.observe(|trigger: Trigger<Combined>| {
match trigger.event() {
MyEvent::Foo { foo } => { /* ... */ },
MyEvent::Bar { bar } => { /* ... */ },
}
}); |
Notably, this would require essentially no changes. Just an implementation of the |
Just put together a PR for consideration, given how simple this was to implement. |
Before we go too deep down this rabbit hole, can we discuss actual use cases? It would be good to come up with some real-world scenarios to illustrate why this is necessary. |
Here is a real-world use case that came up for me: If I understand correctly, #14674 would not solve this particular case since I couldn't listen to two components at once? |
This is just an observation, but if the eventual plan is to use observers to keep query caches updated, those observers would know when archetypes start/stop matching and could share those events as triggers for other observers. In the use case described in the previous comment, a hypothetical observer keeping some You might point out that ambiguity between many same/similar queries would be an issue, but that issue would simply disappear if both components and queries become entities (and We can see that asking if entity If components and queries are both entities, you could actually start thinking of queries as "inferred components" (more commonly known as rules), i.e. flecs has a pattern like this it calls "monitors" and an interesting way people use it is to subscribe to queries that express "invalid" bundles. That way, they can see if entities are getting into bad states. |
What problem does this solve or what need does it fill?
Being able to trigger the same observer with multiple different event types would be useful for code reuse and centralizing related logic. This is different from
enum
Event types, which don't allow you to listen to individual variants for separate observers.What solution would you like?
What alternative(s) have you considered?
Use an enum:
However, that prevents us from listening to only 1 of the event types, or a subset of them.
Additional context
This was mentioned on discord as currently possible, albeit with unsafe APIs, so we should introduce a safe wrapper:
The text was updated successfully, but these errors were encountered: