Skip to content

Commit 313db39

Browse files
authored
Add try_insert_with_new (#14787)
# Objective Fix #14771 by adding a `try_insert_if_new` method to the `EntityCommands` ## Solution This simply calls the `try_insert` function with `InsertMode::Keep` ## Testing I did not add any test because `EntityCommands::try_insert` does not seem to be tested either. I can add some if needed.
1 parent ae74df3 commit 313db39

File tree

1 file changed

+14
-1
lines changed
  • crates/bevy_ecs/src/system/commands

1 file changed

+14
-1
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ impl EntityCommands<'_> {
960960
/// The command will panic when applied if the associated entity does not
961961
/// exist.
962962
///
963-
/// To avoid a panic in this case, use the command [`Self::try_insert`]
963+
/// To avoid a panic in this case, use the command [`Self::try_insert_if_new`]
964964
/// instead.
965965
pub fn insert_if_new(&mut self, bundle: impl Bundle) -> &mut Self {
966966
self.add(insert(bundle, InsertMode::Keep))
@@ -1065,6 +1065,19 @@ impl EntityCommands<'_> {
10651065
self.add(try_insert(bundle, InsertMode::Replace))
10661066
}
10671067

1068+
/// Tries to add a [`Bundle`] of components to the entity without overwriting.
1069+
///
1070+
/// This is the same as [`EntityCommands::try_insert`], but in case of duplicate
1071+
/// components will leave the old values instead of replacing them with new
1072+
/// ones.
1073+
///
1074+
/// # Note
1075+
///
1076+
/// Unlike [`Self::insert_if_new`], this will not panic if the associated entity does not exist.
1077+
pub fn try_insert_if_new(&mut self, bundle: impl Bundle) -> &mut Self {
1078+
self.add(try_insert(bundle, InsertMode::Keep))
1079+
}
1080+
10681081
/// Removes a [`Bundle`] of components from the entity.
10691082
///
10701083
/// # Example

0 commit comments

Comments
 (0)