Skip to content

Allow Events to opt out of being targeted or untargeted for Observers #14843

Open
@ItsDoot

Description

@ItsDoot

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

Not all Event types can work well when targeted at an entity, or not targeted at an entity. This can end up surprising to users of these event types when they don't read the event's documentation about this, or if the documentation doesn't exist in the first place.

What solution would you like?

Allow Events to opt out of having a target or not having a target by providing two new subtraits:

trait TargetedEvent: Event {}
trait UntargetedEvent: Event {}

That are automatically implemented by default for a given type when using #[derive(Event)], but can be opted out of via a macro attribute:

#[derive(Event)]
#[event(untargeted = false)] // Opt out of untargeted, aka Commands::trigger
#[event(targeted = false)] // Opt out of targeted, aka Commands::trigger_targets
struct MyEvent {
}

Then, Commands/World/etc should have their related signatures updated to be:

impl Commands {
    pub fn trigger(&mut self, event: impl UntargetedEvent);
    pub fn trigger_targets(&mut self, event: impl TargetedEvent, targets: impl TriggerTargets);
}

What alternative(s) have you considered?

Add documentation to all event types that are used with observers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-FeatureA new feature, making something new possibleD-MacrosCode that generates Rust codeD-ModestA "normal" level of difficulty; suitable for simple features or challenging fixes

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions