Skip to content

Commit

Permalink
component commands changed by
Browse files Browse the repository at this point in the history
  • Loading branch information
SpecificProtagonist committed Jul 8, 2024
1 parent aebbc27 commit 0eafbcb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
15 changes: 14 additions & 1 deletion crates/bevy_ecs/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,12 +643,25 @@ impl<'w> BundleInserter<'w> {
entity: Entity,
location: EntityLocation,
bundle: T,
) -> EntityLocation {
self.insert_with_caller(entity, location, bundle, *core::panic::Location::caller())
}

/// # Safety
/// `entity` must currently exist in the source archetype for this inserter. `location`
/// must be `entity`'s location in the archetype. `T` must match this [`BundleInfo`]'s type
#[inline]
pub(crate) unsafe fn insert_with_caller<T: DynamicBundle>(
&mut self,
entity: Entity,
location: EntityLocation,
bundle: T,
caller: core::panic::Location<'static>,
) -> EntityLocation {
let bundle_info = self.bundle_info.as_ref();
let add_bundle = self.add_bundle.as_ref();
let table = self.table.as_mut();
let archetype = self.archetype.as_mut();
let caller = *core::panic::Location::caller();

let (new_archetype, new_location) = match &mut self.result {
InsertBundleResult::SameArchetype => {
Expand Down
12 changes: 8 additions & 4 deletions crates/bevy_ecs/src/system/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use crate::{
component::ComponentId,
entity::{Entities, Entity},
system::{RunSystemWithInput, SystemId},
world::command_queue::RawCommandQueue,
world::{Command, CommandQueue, EntityWorldMut, FromWorld, World},
world::{
command_queue::RawCommandQueue, Command, CommandQueue, EntityWorldMut, FromWorld, World,
},
};
use bevy_utils::tracing::{error, info};
pub use parallel_scope::*;
Expand Down Expand Up @@ -340,6 +341,7 @@ impl<'w, 's> Commands<'w, 's> {
///
/// - [`spawn_empty`](Self::spawn_empty) to spawn an entity without any components.
/// - [`spawn_batch`](Self::spawn_batch) to spawn entities with a bundle each.
#[track_caller]
pub fn spawn<T: Bundle>(&mut self, bundle: T) -> EntityCommands {
let mut e = self.spawn_empty();
e.insert(bundle);
Expand Down Expand Up @@ -1202,9 +1204,10 @@ fn despawn(entity: Entity, world: &mut World) {
/// An [`EntityCommand`] that adds the components in a [`Bundle`] to an entity.
#[track_caller]
fn insert<T: Bundle>(bundle: T) -> impl EntityCommand {
let caller = *core::panic::Location::caller();
move |entity: Entity, world: &mut World| {
if let Some(mut entity) = world.get_entity_mut(entity) {
entity.insert(bundle);
entity.insert_with_caller(bundle, caller);
} else {
panic!("error[B0003]: Could not insert a bundle (of type `{}`) for entity {:?} because it doesn't exist in this World. See: https://bevyengine.org/learn/errors/#b0003", std::any::type_name::<T>(), entity);
}
Expand All @@ -1214,9 +1217,10 @@ fn insert<T: Bundle>(bundle: T) -> impl EntityCommand {
/// An [`EntityCommand`] that attempts to add the components in a [`Bundle`] to an entity.
#[track_caller]
fn try_insert(bundle: impl Bundle) -> impl EntityCommand {
let caller = *core::panic::Location::caller();
move |entity, world: &mut World| {
if let Some(mut entity) = world.get_entity_mut(entity) {
entity.insert(bundle);
entity.insert_with_caller(bundle, caller);
}
}
}
Expand Down
13 changes: 12 additions & 1 deletion crates/bevy_ecs/src/world/entity_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,12 +767,23 @@ impl<'w> EntityWorldMut<'w> {
/// This will overwrite any previous value(s) of the same component type.
#[track_caller]
pub fn insert<T: Bundle>(&mut self, bundle: T) -> &mut Self {
self.insert_with_caller(bundle, *core::panic::Location::caller())
}

/// Adds a [`Bundle`] of components to the entity.
///
/// This will overwrite any previous value(s) of the same component type.
pub fn insert_with_caller<T: Bundle>(
&mut self,
bundle: T,
caller: core::panic::Location<'static>,
) -> &mut Self {
let change_tick = self.world.change_tick();
let mut bundle_inserter =
BundleInserter::new::<T>(self.world, self.location.archetype_id, change_tick);
self.location =
// SAFETY: location matches current entity. `T` matches `bundle_info`
unsafe { bundle_inserter.insert(self.entity, self.location, bundle) };
unsafe { bundle_inserter.insert_with_caller(self.entity, self.location, bundle, caller) };
self
}

Expand Down

0 comments on commit 0eafbcb

Please sign in to comment.