Skip to content

Commit

Permalink
Accept Bundles for insert and remove. Deprecate insert/remove_bundle (#…
Browse files Browse the repository at this point in the history
…6039)

# Objective

Take advantage of the "impl Bundle for Component" changes in #2975 / add the follow up changes discussed there.

## Solution

- Change `insert` and `remove` to accept a Bundle instead of a Component (for both Commands and World)
- Deprecate `insert_bundle`, `remove_bundle`, and `remove_bundle_intersection`
- Add `remove_intersection`

---

## Changelog

- Change `insert` and `remove` now accept a Bundle instead of a Component (for both Commands and World)
- `insert_bundle` and `remove_bundle` are deprecated
 

## Migration Guide

Replace `insert_bundle` with `insert`:
```rust
// Old (0.8)
commands.spawn().insert_bundle(SomeBundle::default());
// New (0.9)
commands.spawn().insert(SomeBundle::default());
```

Replace `remove_bundle` with `remove`:
```rust
// Old (0.8)
commands.entity(some_entity).remove_bundle::<SomeBundle>();
// New (0.9)
commands.entity(some_entity).remove::<SomeBundle>();
```

Replace `remove_bundle_intersection` with `remove_intersection`:
```rust
// Old (0.8)
world.entity_mut(some_entity).remove_bundle_intersection::<SomeBundle>();
// New (0.9)
world.entity_mut(some_entity).remove_intersection::<SomeBundle>();
```

Consider consolidating as many operations as possible to improve ergonomics and cut down on archetype moves:
```rust
// Old (0.8)
commands.spawn()
  .insert_bundle(SomeBundle::default())
  .insert(SomeComponent);

// New (0.9) - Option 1
commands.spawn().insert((
  SomeBundle::default(),
  SomeComponent,
))

// New (0.9) - Option 2
commands.spawn_bundle((
  SomeBundle::default(),
  SomeComponent,
))
```

## Next Steps

Consider changing `spawn` to accept a bundle and deprecate `spawn_bundle`.
  • Loading branch information
cart committed Sep 21, 2022
1 parent d9e99cd commit cd15f0f
Show file tree
Hide file tree
Showing 65 changed files with 491 additions and 606 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Benchmark {
entities.push(
world
.spawn()
.insert_bundle((
.insert((
A(Mat4::from_scale(Vec3::ONE)),
B(Mat4::from_scale(Vec3::ONE)),
C(Mat4::from_scale(Vec3::ONE)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl Benchmark {
entities.push(
world
.spawn()
.insert_bundle((
.insert((
A(Mat4::from_scale(Vec3::ONE)),
B(Mat4::from_scale(Vec3::ONE)),
C(Mat4::from_scale(Vec3::ONE)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Benchmark {
pub fn run(&mut self) {
let mut world = World::new();
for _ in 0..10_000 {
world.spawn().insert_bundle((
world.spawn().insert((
Transform(Mat4::from_scale(Vec3::ONE)),
Position(Vec3::X),
Rotation(Vec3::X),
Expand Down
2 changes: 1 addition & 1 deletion benches/benches/bevy_ecs/iteration/iter_frag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ macro_rules! create_entities {
#[derive(Component)]
struct $variants(f32);
for _ in 0..20 {
$world.spawn().insert_bundle(($variants(0.0), Data(1.0)));
$world.spawn().insert(($variants(0.0), Data(1.0)));
}
)*
};
Expand Down
2 changes: 1 addition & 1 deletion benches/benches/bevy_ecs/iteration/iter_frag_foreach.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ macro_rules! create_entities {
#[derive(Component)]
struct $variants(f32);
for _ in 0..20 {
$world.spawn().insert_bundle(($variants(0.0), Data(1.0)));
$world.spawn().insert(($variants(0.0), Data(1.0)));
}
)*
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ macro_rules! create_entities {
#[derive(Component)]
struct $variants(f32);
for _ in 0..20 {
$world.spawn().insert_bundle((
$world.spawn().insert((
$variants(0.0),
Data::<0>(1.0),
Data::<1>(1.0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<'w> Benchmark<'w> {
pub fn new() -> Self {
let mut world = World::new();
for _ in 0..5 {
world.spawn().insert_bundle((
world.spawn().insert((
Data::<0>(1.0),
Data::<1>(1.0),
Data::<2>(1.0),
Expand Down
2 changes: 1 addition & 1 deletion benches/benches/bevy_ecs/iteration/iter_frag_wide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ macro_rules! create_entities {
#[derive(Component)]
struct $variants(f32);
for _ in 0..20 {
$world.spawn().insert_bundle((
$world.spawn().insert((
$variants(0.0),
Data::<0>(1.0),
Data::<1>(1.0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<'w> Benchmark<'w> {
let mut world = World::new();

for _ in 0..5 {
world.spawn().insert_bundle((
world.spawn().insert((
Data::<0>(1.0),
Data::<1>(1.0),
Data::<2>(1.0),
Expand Down
2 changes: 1 addition & 1 deletion benches/benches/bevy_ecs/iteration/iter_simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl<'w> Benchmark<'w> {

// TODO: batch this
for _ in 0..10_000 {
world.spawn().insert_bundle((
world.spawn().insert((
Transform(Mat4::from_scale(Vec3::ONE)),
Position(Vec3::X),
Rotation(Vec3::X),
Expand Down
2 changes: 1 addition & 1 deletion benches/benches/bevy_ecs/iteration/iter_simple_foreach.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl<'w> Benchmark<'w> {

// TODO: batch this
for _ in 0..10_000 {
world.spawn().insert_bundle((
world.spawn().insert((
Transform(Mat4::from_scale(Vec3::ONE)),
Position(Vec3::X),
Rotation(Vec3::X),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl<'w> Benchmark<'w> {

// TODO: batch this
for _ in 0..10_000 {
world.spawn().insert_bundle((
world.spawn().insert((
Transform(Mat4::from_scale(Vec3::ONE)),
Position(Vec3::X),
Rotation(Vec3::X),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl<'w> Benchmark<'w> {

// TODO: batch this
for _ in 0..10_000 {
world.spawn().insert_bundle((
world.spawn().insert((
Transform(Mat4::from_scale(Vec3::ONE)),
Rotation(Vec3::X),
Position::<0>(Vec3::X),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<'w> Benchmark<'w> {

// TODO: batch this
for _ in 0..10_000 {
world.spawn().insert_bundle((
world.spawn().insert((
Transform(Mat4::from_scale(Vec3::ONE)),
Rotation(Vec3::X),
Position::<0>(Vec3::X),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl<'w> Benchmark<'w> {

// TODO: batch this
for _ in 0..10_000 {
world.spawn().insert_bundle((
world.spawn().insert((
Transform(Mat4::from_scale(Vec3::ONE)),
Position(Vec3::X),
Rotation(Vec3::X),
Expand Down
2 changes: 1 addition & 1 deletion benches/benches/bevy_ecs/iteration/iter_simple_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Benchmark {

// TODO: batch this
for _ in 0..10_000 {
world.spawn().insert_bundle((
world.spawn().insert((
Transform(Mat4::from_scale(Vec3::ONE)),
Position(Vec3::X),
Rotation(Vec3::X),
Expand Down
2 changes: 1 addition & 1 deletion benches/benches/bevy_ecs/iteration/iter_simple_wide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl<'w> Benchmark<'w> {

// TODO: batch this
for _ in 0..10_000 {
world.spawn().insert_bundle((
world.spawn().insert((
Transform(Mat4::from_scale(Vec3::ONE)),
Rotation(Vec3::X),
Position::<0>(Vec3::X),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<'w> Benchmark<'w> {

// TODO: batch this
for _ in 0..10_000 {
world.spawn().insert_bundle((
world.spawn().insert((
Transform(Mat4::from_scale(Vec3::ONE)),
Rotation(Vec3::X),
Position::<0>(Vec3::X),
Expand Down
4 changes: 2 additions & 2 deletions benches/benches/bevy_ecs/world/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub fn insert_commands(criterion: &mut Criterion) {
for entity in &entities {
commands
.entity(*entity)
.insert_bundle((Matrix::default(), Vec3::default()));
.insert((Matrix::default(), Vec3::default()));
}
drop(commands);
command_queue.apply(&mut world);
Expand Down Expand Up @@ -238,7 +238,7 @@ pub fn get_or_spawn(criterion: &mut Criterion) {
for i in 0..10_000 {
commands
.get_or_spawn(Entity::from_raw(i))
.insert_bundle((Matrix::default(), Vec3::default()));
.insert((Matrix::default(), Vec3::default()));
}
command_queue.apply(&mut world);
});
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_core_pipeline/src/core_3d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ pub fn extract_core_3d_camera_phases(
) {
for (entity, camera) in &cameras_3d {
if camera.is_active {
commands.get_or_spawn(entity).insert_bundle((
commands.get_or_spawn(entity).insert((
RenderPhase::<Opaque3d>::default(),
RenderPhase::<AlphaMask3d>::default(),
RenderPhase::<Transparent3d>::default(),
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,10 @@ struct PlayerBundle {
let mut world = World::new();

// Spawn a new entity and insert the default PlayerBundle
world.spawn().insert_bundle(PlayerBundle::default());
world.spawn().insert(PlayerBundle::default());

// Bundles play well with Rust's struct update syntax
world.spawn().insert_bundle(PlayerBundle {
world.spawn().insert(PlayerBundle {
position: Position { x: 1.0, y: 1.0 },
..Default::default()
});
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/examples/change_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ enum SimulationSystem {
// If an entity gets spawned, we increase the counter in the EntityCounter resource
fn spawn_entities(mut commands: Commands, mut entity_counter: ResMut<EntityCounter>) {
if rand::thread_rng().gen_bool(0.6) {
let entity_id = commands.spawn().insert(Age::default()).id();
let entity_id = commands.spawn_bundle(Age::default()).id();
println!(" spawning {:?}", entity_id);
entity_counter.value += 1;
}
Expand Down
7 changes: 2 additions & 5 deletions crates/bevy_ecs/src/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,19 @@
//! |Spawn a new entity|[`Commands::spawn`]|[`World::spawn`]|
//! |Spawn an entity with components|[`Commands::spawn_bundle`]|---|
//! |Despawn an entity|[`EntityCommands::despawn`]|[`World::despawn`]|
//! |Insert a component to an entity|[`EntityCommands::insert`]|[`EntityMut::insert`]|
//! |Insert multiple components to an entity|[`EntityCommands::insert_bundle`]|[`EntityMut::insert_bundle`]|
//! |Remove a component from an entity|[`EntityCommands::remove`]|[`EntityMut::remove`]|
//! |Insert a component, bundle, or tuple of components and bundles to an entity|[`EntityCommands::insert`]|[`EntityMut::insert`]|
//! |Remove a component, bundle, or tuple of components and bundles from an entity|[`EntityCommands::remove`]|[`EntityMut::remove`]|
//!
//! [`World`]: crate::world::World
//! [`Commands::spawn`]: crate::system::Commands::spawn
//! [`Commands::spawn_bundle`]: crate::system::Commands::spawn_bundle
//! [`EntityCommands::despawn`]: crate::system::EntityCommands::despawn
//! [`EntityCommands::insert`]: crate::system::EntityCommands::insert
//! [`EntityCommands::insert_bundle`]: crate::system::EntityCommands::insert_bundle
//! [`EntityCommands::remove`]: crate::system::EntityCommands::remove
//! [`World::spawn`]: crate::world::World::spawn
//! [`World::spawn_bundle`]: crate::world::World::spawn_bundle
//! [`World::despawn`]: crate::world::World::despawn
//! [`EntityMut::insert`]: crate::world::EntityMut::insert
//! [`EntityMut::insert_bundle`]: crate::world::EntityMut::insert_bundle
//! [`EntityMut::remove`]: crate::world::EntityMut::remove
mod map_entities;
mod serde;
Expand Down
Loading

0 comments on commit cd15f0f

Please sign in to comment.