Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"with_children" does not support adding single component #277

Closed
Raytwo opened this issue Aug 21, 2020 · 4 comments
Closed

"with_children" does not support adding single component #277

Raytwo opened this issue Aug 21, 2020 · 4 comments
Labels
A-ECS Entities, components, systems, and events C-Enhancement A new feature C-Usability A simple quality-of-life change that makes Bevy easier to use

Comments

@Raytwo
Copy link

Raytwo commented Aug 21, 2020

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!

@karroffel karroffel added A-ECS Entities, components, systems, and events C-Enhancement A new feature labels Aug 21, 2020
@Ratysz
Copy link
Contributor

Ratysz commented Aug 22, 2020

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.

@ryo33
Copy link
Contributor

ryo33 commented Sep 30, 2020

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.

@alice-i-cecile alice-i-cecile added the C-Usability A simple quality-of-life change that makes Bevy easier to use label Feb 17, 2021
@alice-i-cecile
Copy link
Member

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

@alice-i-cecile
Copy link
Member

Fixed in 0.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Enhancement A new feature C-Usability A simple quality-of-life change that makes Bevy easier to use
Projects
None yet
Development

No branches or pull requests

5 participants