Skip to content
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

Make entity observers more explicit #17187

Open
Demiu opened this issue Jan 6, 2025 · 0 comments
Open

Make entity observers more explicit #17187

Demiu opened this issue Jan 6, 2025 · 0 comments
Labels
A-ECS Entities, components, systems, and events C-Feature A new feature, making something new possible D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Needs-Design This issue requires design work to think about how it would best be accomplished

Comments

@Demiu
Copy link
Contributor

Demiu commented Jan 6, 2025

What problem does this solve or what need does it fill?

Right now, whether an observer is a listening globally or for triggers on specific entities is determined when it's added.

// this will create a global one
commands.spawn(Observer::new(listen_system))
// this will create an entity specific one
commands.spawn(Observer::new(listen_system).with_entity(existing_id))

This presents an issue when trying to cache and reuse an observer. There needs to be an entity to watch, but that may be something spawned during gameplay and not in Startup where you would create such an observer.

// In Startup
let observer_id = commands.spawn(Observer::new(listen_system)).id();
commands.insert_resource(MyObserver(observer_id));
// In Update
my_observer_res.0.watch_entity(commands.spawn(/* ... */));

Since the Observer was created with no entities watched it will respond to events on everything, even after a specific entity to watch has been specified.

What solution would you like?

Ability/requirement to explicitly specify an Observer is or isn't targeted/global. Perhaps some way to switch between the two on the same Observer after creation. Control over the despawn on empty rule.

What alternative(s) have you considered?

Right now a workaround is possible:

// In Startup
let dummy = commands.spawn_empty().id();
let observer_id = commands.spawn(Observer::new(listen_system).with_entity(dummy)).id();
commands.insert_resource(MyObserver(observer_id));

But it's not obvious, ugly and it also "disables" the feature that observer is removed after it's watched entities reach zero. Not to mention you have to know the quirk that Observer created with no entities is "global" and that's something you can't even check by querying the Observer

@Demiu Demiu added C-Feature A new feature, making something new possible S-Needs-Triage This issue needs to be labelled labels Jan 6, 2025
@BenjaminBrienen BenjaminBrienen added A-ECS Entities, components, systems, and events S-Needs-Design This issue requires design work to think about how it would best be accomplished D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes and removed S-Needs-Triage This issue needs to be labelled labels Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Feature A new feature, making something new possible D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Needs-Design This issue requires design work to think about how it would best be accomplished
Projects
None yet
Development

No branches or pull requests

2 participants