Skip to content

Allow all hooks on relationship components#24000

Merged
alice-i-cecile merged 3 commits into
bevyengine:mainfrom
SpecificProtagonist:push-nnlsqowsnvsk
Apr 28, 2026
Merged

Allow all hooks on relationship components#24000
alice-i-cecile merged 3 commits into
bevyengine:mainfrom
SpecificProtagonist:push-nnlsqowsnvsk

Conversation

@SpecificProtagonist

@SpecificProtagonist SpecificProtagonist commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

Objective

Fixes #23990

Solution

If both the user and the relationship define a hook, call both.

Testing

Added a test checking hooks don't interfere with relationships.


Showcase

#[derive(Component)]
#[relationship(relationship_target = LikedBy)]
#[component(on_add = on_add)]
struct Likes(Entity);

@SpecificProtagonist SpecificProtagonist added C-Feature A new feature, making something new possible A-ECS Entities, components, systems, and events S-Needs-Review Needs reviewer attention (from anyone!) to move forward D-Macros Code that generates Rust code labels Apr 27, 2026
@github-project-automation github-project-automation Bot moved this to Needs SME Triage in ECS Apr 27, 2026
struct RelTarget(Entity);

fn on_add(world: DeferredWorld, context: HookContext) {
assert!(!world.entity(context.entity).contains::<RelTarget>());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a point of discussion: do we want the user defined hook to run before or after the relationship one?

@SpecificProtagonist SpecificProtagonist Apr 27, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it doesn't matter a lot because structural changes are deferred. Currently the order in which hooks are called when inserting/removing multiple components at once isn't specified either (and they all run before the world is flushed).
I suppose this assert is misleading in this regard as it's independent of the order.

@chescock chescock left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, nice, you managed to get this down to a pretty small change!

Comment thread crates/bevy_ecs/macros/src/component.rs Outdated
Comment thread crates/bevy_ecs/macros/src/component.rs

@alice-i-cecile alice-i-cecile left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, that's incredibly nice. Love to see it!

@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Apr 28, 2026
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Apr 28, 2026
@alice-i-cecile alice-i-cecile mentioned this pull request Apr 28, 2026
1 task
Merged via the queue into bevyengine:main with commit a536222 Apr 28, 2026
40 checks passed
@github-project-automation github-project-automation Bot moved this from Needs SME Triage to Done in ECS Apr 28, 2026
@SpecificProtagonist SpecificProtagonist deleted the push-nnlsqowsnvsk branch April 29, 2026 10:10
tychedelia pushed a commit to processing/bevy that referenced this pull request Apr 30, 2026
# Objective

Fixes bevyengine#23990

## Solution

If both the user and the relationship define a hook, call both.

## Testing

Added a test checking hooks don't interfere with relationships.

---

## Showcase

```rust
#[derive(Component)]
#[relationship(relationship_target = LikedBy)]
#[component(on_add = on_add)]
struct Likes(Entity);
```
tychedelia pushed a commit to processing/bevy that referenced this pull request Apr 30, 2026
# Objective

bevyengine#24000 introduced the ability to use both relationships and
user-provided hooks with `#[derive(Component)]`, but the hook function
that is generated when there's a conflict does not compile.

## Solution

Remove an errant nested `quote!`, and change the inline function
definition to a closure so that `Self` type references work.

## Testing

Updated the test to check the rest of the hooks, and it passes with this
fix.
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-Feature A new feature, making something new possible D-Macros Code that generates Rust code S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Relationship components cannot define their own lifecycle hooks

4 participants