Skip to content

Update .entry() docs to show both insert-then-modify and modify-or-insert examples #19327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions crates/bevy_ecs/src/system/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1231,15 +1231,32 @@ impl<'a> EntityCommands<'a> {
/// #[derive(Component)]
/// struct Level(u32);
///
///
/// #[derive(Component, Default)]
/// struct Mana {
/// max: u32,
/// current: u32,
/// }
///
/// fn level_up_system(mut commands: Commands, player: Res<PlayerEntity>) {
/// // If a component already exists then modify it, otherwise insert a default value
/// 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));
///
/// // Add a default value if none exists, and then modify the existing or new value
/// commands
/// .entity(player.entity)
/// .entry::<Mana>()
/// .or_default()
/// .and_modify(|mut mana| {
/// mana.max += 10;
/// mana.current = mana.max;
/// });
/// }
///
/// # bevy_ecs::system::assert_is_system(level_up_system);
/// ```
pub fn entry<T: Component>(&mut self) -> EntityEntryCommands<T> {
Expand Down