Skip to content

Preserve extra data in RelationshipTarget with replace_related(_with_difference) #19588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 15, 2025

Conversation

urben1680
Copy link
Contributor

@urben1680 urben1680 commented Jun 11, 2025

Objective

The methods and commands replace_related and replace_related_with_difference may cause data stored at the RelationshipTarget be lost when all original children are removed before new children are added.

Part of #19589

Solution

Fix the issue, either by removing the old children after adding the new ones and not before (replace_related_with_difference) or by taking the whole RelationshipTarget to modify it, not only the inner collection (replace_related).

Testing

I added a new test asserting the data is kept. I also added a general test of these methods as they had none previously.

@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior A-ECS Entities, components, systems, and events X-Uncontroversial This work is generally agreed upon S-Needs-Review Needs reviewer attention (from anyone!) to move forward D-Straightforward Simple bug fixes and API improvements, docs, test and examples labels Jun 11, 2025
Copy link
Member

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

Choose a reason for hiding this comment

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

Good; thanks for the tests here. This is too tricky to be confident without them.

Copy link
Contributor

@gwafotapa gwafotapa left a comment

Choose a reason for hiding this comment

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

Looks good to me! I would just like to see the dead code disappear.

On a side note, I'm not sure why replace_related_with_difference needs the entities_to_relate argument.

@alice-i-cecile alice-i-cecile added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jun 14, 2025
@urben1680
Copy link
Contributor Author

urben1680 commented Jun 15, 2025

On a side note, I'm not sure why replace_related_with_difference needs the entities_to_relate argument.

It is more efficient to reinsert them after clearing the RelationshipTarget. Otherwise you would have to call RelationshipSourceCollection::remove in a loop which is quite inefficient for Vec<Entity>. It is a better idea to just insert them again, also because the typical Vec<Entity> can be easily extended via a slice.

Also you need it in case the RelationshipTarget is missing despite the caller's assumption. If the first reason was not present, this would have to panic in that case.

Good call on the branches. 👍

@urben1680 urben1680 requested a review from gwafotapa June 15, 2025 13:08
@gwafotapa
Copy link
Contributor

Ah I see. Thank you.

@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-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Jun 15, 2025
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jun 15, 2025
Merged via the queue into bevyengine:main with commit 8bce3e4 Jun 15, 2025
34 checks passed
@urben1680 urben1680 deleted the replace_related_keeps_data branch June 15, 2025 17:01
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-Bug An unexpected or incorrect behavior D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it X-Uncontroversial This work is generally agreed upon
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants