Skip to content

New Event/Observer API is unfriendly to Entity-like types #21384

@Zeenobit

Description

@Zeenobit

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

Hello! I maintain the moonshine-kind crate, which adds the Instance<T> type. The new changes for entity events in Bevy 0.17 make hard assumptions that the underlying target is exactly an Entity and nothing else.

This makes implementation of newtype wrappers around Entity (like Instance<T>) very awkward.

For example:

  • The EntityEvent trait demands a &mut Entity access which forces any Entity-like struct to provide mutable entity access. This could be avoided if this trait had a set_event_target method instead of event_target_mut.
  • The EntityEvent derive macro expects exactly an Entity type. In my opinion, it should work with any ContainsEntity type.
  • The EntityCommands::trigger method demands an impl FnOnce(Entity) constructor function. There should also be a function that lets you just trigger any EntityEvent as-is. I don't see the need to force the user to pass a constructor instead of the event itself.

What solution would you like?

  • Refactor EntityEvent::event_target_mut to EntityEvent::set_event_target
  • Support ContainsEntity on #[derive(EntityEvent)]
  • Refactor EntityCommands::trigger to take an impl EntityEvent, and add trigger_with as the ctor version. trigger_with should also support ContainsEntity.

What alternative(s) have you considered?

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-UsabilityA targeted quality-of-life change that makes Bevy easier to useD-ModestA "normal" level of difficulty; suitable for simple features or challenging fixesS-Ready-For-ImplementationThis issue is ready for an implementation PR. Go for it!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions