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.
This commit is contained in:
nsarlin 2024-08-16 23:25:11 +02:00 committed by GitHub
parent ae74df3464
commit 313db39912
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -960,7 +960,7 @@ impl EntityCommands<'_> {
/// The command will panic when applied if the associated entity does not
/// exist.
///
/// To avoid a panic in this case, use the command [`Self::try_insert`]
/// To avoid a panic in this case, use the command [`Self::try_insert_if_new`]
/// instead.
pub fn insert_if_new(&mut self, bundle: impl Bundle) -> &mut Self {
self.add(insert(bundle, InsertMode::Keep))
@ -1065,6 +1065,19 @@ impl EntityCommands<'_> {
self.add(try_insert(bundle, InsertMode::Replace))
}
/// Tries to add a [`Bundle`] of components to the entity without overwriting.
///
/// This is the same as [`EntityCommands::try_insert`], but in case of duplicate
/// components will leave the old values instead of replacing them with new
/// ones.
///
/// # Note
///
/// Unlike [`Self::insert_if_new`], this will not panic if the associated entity does not exist.
pub fn try_insert_if_new(&mut self, bundle: impl Bundle) -> &mut Self {
self.add(try_insert(bundle, InsertMode::Keep))
}
/// Removes a [`Bundle`] of components from the entity.
///
/// # Example