Added Convenience Method For Entity Mutation#9602
Added Convenience Method For Entity Mutation#9602bushrat011899 wants to merge 1 commit intobevyengine:mainfrom
Conversation
We can just remove the impl for |
There was a problem hiding this comment.
I like this much better than the solution in #9544.
I'm not a fan of the repeated entity in the method call, I still like the mutate name best of the options :p
That said, I would actually just follow @JoJoJet's advice above, and break the previous impl in favor of this! It's much more reasonable, and it's good to clean up old bits of the API that didn't turn out useful in practice.
# Objective - Fixes #4917 - Replaces #9602 ## Solution - Replaced `EntityCommand` implementation for `FnOnce` to apply to `FnOnce(EntityMut)` instead of `FnOnce(Entity, &mut World)` --- ## Changelog - `FnOnce(Entity, &mut World)` no longer implements `EntityCommand`. This is a breaking change. ## Migration Guide ### 1. New-Type `FnOnce` Create an `EntityCommand` type which implements the method you previously wrote: ```rust pub struct ClassicEntityCommand<F>(pub F); impl<F> EntityCommand for ClassicEntityCommand<F> where F: FnOnce(Entity, &mut World) + Send + 'static, { fn apply(self, id: Entity, world: &mut World) { (self.0)(id, world); } } commands.add(ClassicEntityCommand(|id: Entity, world: &mut World| { /* ... */ })); ``` ### 2. Extract `(Entity, &mut World)` from `EntityMut` The method `into_world_mut` can be used to gain access to the `World` from an `EntityMut`. ```rust let old = |id: Entity, world: &mut World| { /* ... */ }; let new = |mut entity: EntityMut| { let id = entity.id(); let world = entity.into_world_mut(); /* ... */ }; ```
# Objective - Fixes bevyengine#4917 - Replaces bevyengine#9602 ## Solution - Replaced `EntityCommand` implementation for `FnOnce` to apply to `FnOnce(EntityMut)` instead of `FnOnce(Entity, &mut World)` --- ## Changelog - `FnOnce(Entity, &mut World)` no longer implements `EntityCommand`. This is a breaking change. ## Migration Guide ### 1. New-Type `FnOnce` Create an `EntityCommand` type which implements the method you previously wrote: ```rust pub struct ClassicEntityCommand<F>(pub F); impl<F> EntityCommand for ClassicEntityCommand<F> where F: FnOnce(Entity, &mut World) + Send + 'static, { fn apply(self, id: Entity, world: &mut World) { (self.0)(id, world); } } commands.add(ClassicEntityCommand(|id: Entity, world: &mut World| { /* ... */ })); ``` ### 2. Extract `(Entity, &mut World)` from `EntityMut` The method `into_world_mut` can be used to gain access to the `World` from an `EntityMut`. ```rust let old = |id: Entity, world: &mut World| { /* ... */ }; let new = |mut entity: EntityMut| { let id = entity.id(); let world = entity.into_world_mut(); /* ... */ }; ```
# Objective - Fixes bevyengine#4917 - Replaces bevyengine#9602 ## Solution - Replaced `EntityCommand` implementation for `FnOnce` to apply to `FnOnce(EntityMut)` instead of `FnOnce(Entity, &mut World)` --- ## Changelog - `FnOnce(Entity, &mut World)` no longer implements `EntityCommand`. This is a breaking change. ## Migration Guide ### 1. New-Type `FnOnce` Create an `EntityCommand` type which implements the method you previously wrote: ```rust pub struct ClassicEntityCommand<F>(pub F); impl<F> EntityCommand for ClassicEntityCommand<F> where F: FnOnce(Entity, &mut World) + Send + 'static, { fn apply(self, id: Entity, world: &mut World) { (self.0)(id, world); } } commands.add(ClassicEntityCommand(|id: Entity, world: &mut World| { /* ... */ })); ``` ### 2. Extract `(Entity, &mut World)` from `EntityMut` The method `into_world_mut` can be used to gain access to the `World` from an `EntityMut`. ```rust let old = |id: Entity, world: &mut World| { /* ... */ }; let new = |mut entity: EntityMut| { let id = entity.id(); let world = entity.into_world_mut(); /* ... */ }; ```
Objective
Solution
for_entity_muttoEntityCommandswhich allows mutable access to the targetEntity. This method is implemented as discussed in #9544Notes
addmethod, butFnOnce(Entity, &mut World)already implementedEntityCommand, it is not (currently) possible to also implement it forFnOnce(EntityMut)as well. In the future, it may be possible to overcome this limitation through variadic functions.for_each_mutover a single itemEntityMut, hencefor_entity_mut. The original proposed name wasmutate, which I personally felt might have been ambiguous (e.g., are we mutating the command list, the world, etc.)