Skip to content

Cannot easily spawn an entity with one component #607

Closed

Description

@smokku

If I try spawning an entity with just one component, I get the following error:

error[E0277]: the trait bound `Pawn: bevy::bevy_ecs::DynamicBundle` is not satisfied
  --> examples/balls.rs:69:20
   |
69 |     commands.spawn(Pawn { controller: 1 }).with(Ball {
   |                    ^^^^^^^^^^^^^^^^^^^^^^ the trait `bevy::bevy_ecs::DynamicBundle` is not implemented for `Pawn`

I need to spawn a one element tuple:

commands.spawn((Pawn { controller: 1 },)).with(Ball {

There is no way of coming up to that solution from the error message.

Bevy's selling point is ECS ergonomics - i think this needs to be fixed.

Activity

  1. added
    A-ECSEntities, components, systems, and events
    C-FeatureA new feature, making something new possible
    on Sep 30, 2020
  2. memoryruins commented on Sep 30, 2020

    @memoryruins
    Contributor

    Since there are multiple components in your example, could do .spawn((Pawn, Ball)), but I understand there can be a preference to use the methods since it allows quickly commenting out withs. spawn(()).with(component) works but also isn't entirely ergonomic.

    We could add a method like Commands::spawn_with(&mut self, component: impl Component), and then spawn could be renamed to spawn_with_bundle for clarity.

    To add spawn_with to your app today, an option is to use a function or add this to an extension trait.

    trait CommandsExtension {
        fn spawn_with(&mut self, components: impl Component) -> &mut Self;
    }
    
    impl CommandsExtension for Commands {
        fn spawn_with(&mut self, component: impl Component) -> &mut Self {
            self.spawn((component,));
            self
        }
    }
    
    fn system(mut commands: Commands) {
        commands.spawn_with(Pawn);
    }

    Note: make sure during bevy upgrades that any new inherent methods on Commands don't shadow your extension methods' names since Rust will prefer the inherent method first.

  3. cart commented on Oct 1, 2020

    @cart
    Member

    Yeah there is unfortunately no way to make spawn() work with both tuples and individual components (because a component could be, itself a tuple). So they are ambiguous to rustc.

    I like @memoryruins idea to make "bundles" a clear part of the api and to add a new api for spawning single components.

  4. smokku commented on Oct 1, 2020

    @smokku
    MemberAuthor

    Thank you for the explanation.
    I also like the idea of making the bundle spawning more clear. "Explicit is better than implicit."

    BTW, I didn't know you can extend an imported type like that. Thank you @memoryruins for this example. :-)

  5. CleanCut commented on Oct 1, 2020

    @CleanCut
    Member

    This sounds like a fun change to make, so I figured I'd give it a shot. Please take a look at #611 and let me know if I'm going in the right direction (naming is hard).

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

    @alice-i-cecile
    Member

    This is fixed now that #611 is merged in and can be closed.

  7. memoryruins commented on Jan 16, 2021

    @memoryruins
    Contributor

    #611 was closed, not merged. The discussion in it contains good points to consider when this issue is picked back up again.

  8. added
    C-UsabilityA targeted quality-of-life change that makes Bevy easier to use
    on Feb 17, 2021
  9. alice-i-cecile commented on Mar 7, 2021

    @alice-i-cecile
    Member

    This should be greatly improved or closed by #1562.

  10. ledyba commented on Apr 1, 2021

    @ledyba

    #1703 (seems improved version of #1562 ) has been closed.
    So was this issue solved?

  11. alice-i-cecile commented on Apr 1, 2021

    @alice-i-cecile
    Member

    Yes, you can now do:

    commands.spawn().insert(MyComponent)

    No weird tuple shenanigans needed.

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