Skip to content

Relationship components cannot define their own lifecycle hooks #23990

Description

@Freyja-moth

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

As it stands there can only be one component hook per component.

While this is an acceptable limitation for most components, it is very limiting when working with relationships, as Bevy's relationship implementation uses component hooks to set things up and enforce invariants.

What solution would you like?

With the introduction of components as entities (#23988) we could reasonably model this with relationships

world.spawn((
    Component,
    related!(ComponentHooks[
        (OnAdd, ComponentHook(|_world, _ctx| {})
        (OnAdd, ComponentHook(|_world, _ctx| {})
    ])
));

What alternative(s) have you considered?

Do nothing

Keep things as they are, it's not a massive deal that relationships can't have custom component hooks, but it might make things trickier if we want to introduce more built-in component hooks

Alternative relationship design

Change the design of relations around component hooks, so that they do not take up the only component hook slot.

Additive hooks

Weaken the "one hook" constraint, by only allowing "additive" hooks, chaining together function calls but only storing a single function pointer.

Make defining hooks for relationship types Just Work by automatically converting e.g. derive macro hooks into this form.

Additional context

#12647 was opened previously intending to do the same thing but was closed since the cases described were already covered by observers, but I don't believe relationships were mentioned during the conversation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-FeatureA new feature, making something new possibleS-Needs-DesignThis issue requires design work to think about how it would best be accomplishedX-Needs-SMEThis type of work requires an SME to approve it.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions