Skip to content

"with_children" does not support adding single component #277

Closed

Description

@Raytwo

Currently, the implementation requires that the component being spawned implemented DynamicBundle, so you can't do something like this:

fn setup(
    mut commands: Commands,
) {
    commands
        // Player
        .spawn(SpriteComponents {
            material: materials.add(Color::rgb(0.5, 0.0, 0.0).into()),
            translation: Translation(Vec3::new(window_desc.width as f32 / 2.0, 50.0, 0.0)),
            sprite: Sprite {
                size: Vec2::new(30.0, 30.0),
            },
            ..Default::default()
        })
        .with(Player::new())
        .with_children(|parent| {
            parent.spawn(BulletEmitter::new());
        });
}

Instead, you need to do it that way:

.with_children(|parent| {
            parent.spawn( (BulletEmitter::new(), ) );
        });
}

While this is a decent workaround, could it be possible to make it so you can use a regular Component too?

A friend suggested implementing the following, although I'm not knowledgeable about Rust enough to do so myself:
DynamicBundle for T: Component

Thanks!

Activity

  1. added
    A-ECSEntities, components, systems, and events
    C-FeatureA new feature, making something new possible
    on Aug 21, 2020
  2. Ratysz commented on Aug 22, 2020

    @Ratysz
    Contributor

    This will run into duplicate implementation, partially because we don't have trait specialization (yet, potentially ever). T: Component bound means everything that can be a component - including all the tuples that DynamicBundle is implemented for elsewhere.

    It can be circumvented with a "marker" generic parameter in DynamicBundle trait, but this could pollute all APIs that use DynamicBundle and potentially Bundle with that rather confusing parameter. I'm almost certain it won't come up in normal use of the public API, but it would show up in documentation.

  3. ryo33 commented on Sep 30, 2020

    @ryo33
    Contributor

    I thought about .with_child(BulletEmitter::new()), which wraps the current with_children, but it's too specific.

    In that case, I prefer to;

    • define BulletEmitterComponents.
    • implement to_bundle(self) for BulletEmitter if it seems never used with other components.
  4. added
    C-UsabilityA targeted quality-of-life change that makes Bevy easier to use
    on Feb 17, 2021
  5. alice-i-cecile commented on Apr 3, 2021

    @alice-i-cecile
    Member

    This should be solved with the new Commands API in 0.5. See #1703.

  6. alice-i-cecile commented on Apr 16, 2021

    @alice-i-cecile
    Member

    Fixed in 0.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-FeatureA new feature, making something new possibleC-UsabilityA targeted quality-of-life change that makes Bevy easier to use

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions