Skip to content

Commit 16a286d

Browse files
authored
Update .entry() docs to show both insert-then-modify and modify-or-insert examples (#19327)
# Objective Fix #16379
1 parent 8a223be commit 16a286d

File tree

1 file changed

+19
-2
lines changed
  • crates/bevy_ecs/src/system/commands

1 file changed

+19
-2
lines changed

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,15 +1273,32 @@ impl<'a> EntityCommands<'a> {
12731273
/// #[derive(Component)]
12741274
/// struct Level(u32);
12751275
///
1276+
///
1277+
/// #[derive(Component, Default)]
1278+
/// struct Mana {
1279+
/// max: u32,
1280+
/// current: u32,
1281+
/// }
1282+
///
12761283
/// fn level_up_system(mut commands: Commands, player: Res<PlayerEntity>) {
1284+
/// // If a component already exists then modify it, otherwise insert a default value
12771285
/// commands
12781286
/// .entity(player.entity)
12791287
/// .entry::<Level>()
1280-
/// // Modify the component if it exists.
12811288
/// .and_modify(|mut lvl| lvl.0 += 1)
1282-
/// // Otherwise, insert a default value.
12831289
/// .or_insert(Level(0));
1290+
///
1291+
/// // Add a default value if none exists, and then modify the existing or new value
1292+
/// commands
1293+
/// .entity(player.entity)
1294+
/// .entry::<Mana>()
1295+
/// .or_default()
1296+
/// .and_modify(|mut mana| {
1297+
/// mana.max += 10;
1298+
/// mana.current = mana.max;
1299+
/// });
12841300
/// }
1301+
///
12851302
/// # bevy_ecs::system::assert_is_system(level_up_system);
12861303
/// ```
12871304
pub fn entry<T: Component>(&mut self) -> EntityEntryCommands<T> {

0 commit comments

Comments
 (0)