Skip to content

Make entity observers more explicit #17187

Closed as duplicate of#19564
Closed as duplicate of#19564
@Demiu

Description

@Demiu

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-FeatureA new feature, making something new possibleD-ModestA "normal" level of difficulty; suitable for simple features or challenging fixesS-Needs-DesignThis issue requires design work to think about how it would best be accomplished

    Type

    No type

    Projects

    Status

    Observer overhaul

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions