Skip to content

Make ChildOf a named struct and support named struct Relationship types #17896

@alice-i-cecile

Description

@alice-i-cecile

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

As discussed in #17886, some users find the new semantics and API of ChildOf frustrating and confusing (see #17247 for that change).

What solution would you like?

@cart and I think that we can improve the developer experience by swapping from a tupled struct to a named struct:

struct ChildOf{
    parent: Entity
}

This helps clarify what's going on by giving a name to the data that's being stored.

In order to do that, you'll need to:

  1. Modify the Relationship derive code (actually part of the Component macro) to work with named structs with a single Entity field.
  2. Add a doc test for this.
  3. Refactor the ChildOf component and clean up all of the breakage.
  4. Rename a few variables to parent to make the code cleaner.

Take a peek at the Deref / DerefMut derives for tips on implementation, although you don't need to get fancy with attributes (see below).

What alternative(s) have you considered?

When this idea was initially proposed, Cart and I thought that marking a specific field as #[relationship_target] would be helpful to increase flexibility.

This isn't needed for the initial PR. Moreover, implementing Relationship::from in the derive for arbitrary structs which might contain data is not particularly feasible without adding quite a bit more complexity.

Don't bother with it for now! Advanced users can do a manual impl for now.

Additional context

I started tackling this today, hence the detailed notes. I'm not great at macros though, so I didn't finish it and I'm shouting into the void in the hopes that one of our lovely contributors finishes it. Bother me if you want to see my draft branch, but it's not particularly helpful beyond these notes.

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 useD-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-BlessedHas a large architectural impact or tradeoffs, but the design has been endorsed by decision makers

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions