Skip to content

Support fields with bundle effects in Bundle derive macro #19136

Open
@nfagerlund

Description

@nfagerlund

What problem does this solve or what need does it fill?

There's some nice new spawning ergonomics for spawning a hierarchy of entities all at once. However, I can't put a SpawnRelatedBundle into a field of a struct that I'm deriving Bundle for:

#[derive(Bundle)]
pub struct EnemyDingusBundle {
    pub identity: EnemyDingus,
    pub script_state: SpawnRelatedBundle<ChildOf, Spawn<ScriptState>>
}

impl EnemyDingusBundle {
    fn new(script: Handle<Script>) -> Self {
        Self {
            identity: EnemyDingus,
            script_state: Children::spawn(Spawn(ScriptState::root(script))),
        }
    }
}

This gives an error like the trait bound SpawnRelatedBundle<ChildOf, Spawn<SomeComponent>>: BundleFromComponents is not satisfied.

Putting the SpawnRelatedBundle beside a derived bundle struct and wrapping them in a tuple bundle works fine, though:

#[derive(Bundle)]
pub struct EnemyDingusBundle {
    pub identity: EnemyDingus,
}

impl EnemyDingusBundle {
  fn dingus_plus(script: ScriptState) -> impl Bundle {
    (
        Self { identity: EnemyDingus },
        Children::spawn(Spawn(ScriptState::root(script))),
    )
  }
}

It looks like bundles with bundle effects work as expected in tuple bundles because those have a manual/bespoke Bundle implementation, but the derive macro doesn't know what to do with them.

What solution would you like?

Support effectful bundles as fields of structs that derive Bundle, so that struct bundles have equal features to tuple bundles.

What alternative(s) have you considered?

  • Make do with tuple bundle wrappers instead of well-defined bundle structs. (Which is what I'm going forward with for now.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-FeatureA new feature, making something new possibleD-MacrosCode that generates Rust codeD-ModestA "normal" level of difficulty; suitable for simple features or challenging fixesS-Ready-For-ImplementationThis issue is ready for an implementation PR. Go for it!X-UncontroversialThis work is generally agreed upon

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions