feat(ecs): add EntityEntryCommands::entity() method chaining (#17580)

This allows you to continue chaining method calls after calling
`EntityCommands::entry`:

```rust
commands
    .entity(player.entity)
    .entry::<Level>()
    // Modify the component if it exists
    .and_modify(|mut lvl| lvl.0 += 1)
    // Otherwise insert a default value
    .or_insert(Level(0))
    // Return the EntityCommands for the entity
    .entity()
    // And continue chaining method calls
    .insert(Name::new("Player"));
```

---------

Signed-off-by: Jean Mertz <git@jeanmertz.com>
This commit is contained in:
Jean Mertz 2025-01-29 18:36:02 +01:00 committed by GitHub
parent d4356062bf
commit b58eda01e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2169,6 +2169,38 @@ impl<'a, T: Component> EntityEntryCommands<'a, T> {
.queue(entity_command::insert_from_world::<T>(InsertMode::Keep));
self
}
/// Get the [`EntityCommands`] from which the [`EntityEntryCommands`] was initiated.
///
/// This allows you to continue chaining method calls after calling [`EntityCommands::entry`].
///
/// # Example
///
/// ```
/// # use bevy_ecs::prelude::*;
/// # #[derive(Resource)]
/// # struct PlayerEntity { entity: Entity }
/// #[derive(Component)]
/// struct Level(u32);
///
/// fn level_up_system(mut commands: Commands, player: Res<PlayerEntity>) {
/// commands
/// .entity(player.entity)
/// .entry::<Level>()
/// // Modify the component if it exists
/// .and_modify(|mut lvl| lvl.0 += 1)
/// // Otherwise insert a default value
/// .or_insert(Level(0))
/// // Return the EntityCommands for the entity
/// .entity()
/// // And continue chaining method calls
/// .insert(Name::new("Player"));
/// }
/// # bevy_ecs::system::assert_is_system(level_up_system);
/// ```
pub fn entity(&mut self) -> EntityCommands {
self.entity_commands.reborrow()
}
}
#[cfg(test)]
@ -2258,6 +2290,10 @@ mod tests {
commands.entity(entity).entry::<W<String>>().or_from_world();
queue.apply(&mut world);
assert_eq!("*****", &world.get::<W<String>>(entity).unwrap().0);
let mut commands = Commands::new(&mut queue, &world);
let id = commands.entity(entity).entry::<W<u64>>().entity().id();
queue.apply(&mut world);
assert_eq!(id, entity);
}
#[test]