mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +00:00
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:
parent
ae74df3464
commit
313db39912
1 changed files with 14 additions and 1 deletions
|
@ -960,7 +960,7 @@ impl EntityCommands<'_> {
|
||||||
/// The command will panic when applied if the associated entity does not
|
/// The command will panic when applied if the associated entity does not
|
||||||
/// exist.
|
/// 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.
|
/// instead.
|
||||||
pub fn insert_if_new(&mut self, bundle: impl Bundle) -> &mut Self {
|
pub fn insert_if_new(&mut self, bundle: impl Bundle) -> &mut Self {
|
||||||
self.add(insert(bundle, InsertMode::Keep))
|
self.add(insert(bundle, InsertMode::Keep))
|
||||||
|
@ -1065,6 +1065,19 @@ impl EntityCommands<'_> {
|
||||||
self.add(try_insert(bundle, InsertMode::Replace))
|
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.
|
/// Removes a [`Bundle`] of components from the entity.
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
|
|
Loading…
Reference in a new issue