Skip to content

Commit 227c222

Browse files
cartalice-i-cecileakimakinaijanhohenheim
authored
Add closure-based EntityEvent triggers (#21284)
# Objective Some developers would prefer being able to trigger EntityEvents in the old "chained" way. ## Solution Add support for the following: ```rust commands.entity(e1).trigger(|entity| Explode { entity }) // alternatively if From<Entity> is implemented commands.entity(e1).trigger(Explode::from) // alternatively if it is Explode(Entity) commands.entity(e1).trigger(Explode) ``` --------- Co-authored-by: Alice Cecile <[email protected]> Co-authored-by: akimakinai <[email protected]> Co-authored-by: Jan Hohenheim <[email protected]>
1 parent 633f14b commit 227c222

File tree

2 files changed

+55
-16
lines changed

2 files changed

+55
-16
lines changed

crates/bevy_ecs/src/system/commands/mod.rs

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,17 +2259,49 @@ impl<'a> EntityCommands<'a> {
22592259
self.queue(entity_command::move_components::<B>(target))
22602260
}
22612261

2262-
/// Deprecated. Use [`Commands::trigger`] instead.
2262+
/// Passes the current entity into the given function, and triggers the [`EntityEvent`] returned by that function.
2263+
///
2264+
/// # Example
2265+
///
2266+
/// A surprising number of functions meet the trait bounds for `event_fn`:
2267+
///
2268+
/// ```rust
2269+
/// # use bevy_ecs::prelude::*;
2270+
///
2271+
/// #[derive(EntityEvent)]
2272+
/// struct Explode(Entity);
2273+
///
2274+
/// impl From<Entity> for Explode {
2275+
/// fn from(entity: Entity) -> Self {
2276+
/// Explode(entity)
2277+
/// }
2278+
/// }
2279+
///
2280+
///
2281+
/// fn trigger_via_constructor(mut commands: Commands) {
2282+
/// // The fact that `Epxlode` is a single-field tuple struct
2283+
/// // ensures that `Explode(entity)` is a function that generates
2284+
/// // an EntityEvent, meeting the trait bounds for `event_fn`.
2285+
/// commands.spawn_empty().trigger(Explode);
2286+
///
2287+
/// }
2288+
///
2289+
///
2290+
/// fn trigger_via_from_trait(mut commands: Commands) {
2291+
/// // This variant also works for events like `struct Explode { entity: Entity }`
2292+
/// commands.spawn_empty().trigger(Explode::from);
2293+
/// }
2294+
///
2295+
/// fn trigger_via_closure(mut commands: Commands) {
2296+
/// commands.spawn_empty().trigger(|entity| Explode(entity));
2297+
/// }
2298+
/// ```
22632299
#[track_caller]
2264-
#[deprecated(
2265-
since = "0.17.0",
2266-
note = "Use Commands::trigger with an EntityEvent instead."
2267-
)]
2268-
pub fn trigger<'t, E: EntityEvent>(
2300+
pub fn trigger<'t, E: EntityEvent<Trigger<'t>: Default>>(
22692301
&mut self,
2270-
event: impl EntityEvent<Trigger<'t>: Default>,
2302+
event_fn: impl FnOnce(Entity) -> E,
22712303
) -> &mut Self {
2272-
log::warn!("EntityCommands::trigger is deprecated and no longer triggers the event for the current EntityCommands entity. Use Commands::trigger instead with an EntityEvent.");
2304+
let event = (event_fn)(self.entity);
22732305
self.commands.trigger(event);
22742306
self
22752307
}

crates/bevy_ecs/src/world/entity_ref.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3165,16 +3165,23 @@ impl<'w> EntityWorldMut<'w> {
31653165
})
31663166
}
31673167

3168-
/// Deprecated. Use [`World::trigger`] instead.
3168+
/// Passes the current entity into the given function, and triggers the [`EntityEvent`] returned by that function.
3169+
/// See [`EntityCommands::trigger`] for usage examples
3170+
///
3171+
/// [`EntityCommands::trigger`]: crate::system::EntityCommands::trigger
31693172
#[track_caller]
3170-
#[deprecated(
3171-
since = "0.17.0",
3172-
note = "Use World::trigger with an EntityEvent instead."
3173-
)]
3174-
pub fn trigger<'t>(&mut self, event: impl EntityEvent<Trigger<'t>: Default>) -> &mut Self {
3175-
log::warn!("EntityWorldMut::trigger is deprecated and no longer triggers the event for the current EntityWorldMut entity. Use World::trigger instead with an EntityEvent.");
3173+
pub fn trigger<'t, E: EntityEvent<Trigger<'t>: Default>>(
3174+
&mut self,
3175+
event_fn: impl FnOnce(Entity) -> E,
3176+
) -> &mut Self {
3177+
let mut event = (event_fn)(self.entity);
3178+
let caller = MaybeLocation::caller();
31763179
self.world_scope(|world| {
3177-
world.trigger(event);
3180+
world.trigger_ref_with_caller(
3181+
&mut event,
3182+
&mut <E::Trigger<'_> as Default>::default(),
3183+
caller,
3184+
);
31783185
});
31793186
self
31803187
}

0 commit comments

Comments
 (0)