Skip to content
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

Add EntityCommands.retain and EntityWorldMut.retain #10873

Merged
merged 6 commits into from
Dec 5, 2023

Conversation

13ros27
Copy link
Contributor

@13ros27 13ros27 commented Dec 5, 2023

Objective

Adds EntityCommands.retain and EntityWorldMut.retain to remove all components except the given bundle from the entity.
Fixes #10865.

Solution

I added a private unsafe function in EntityWorldMut called remove_bundle_info which performs the shared behaviour of remove and retain, namely taking a BundleInfo of components to remove, and removing them from the given entity. Then retain simply gets all the components on the entity and filters them by whether they are in the bundle it was passed, before passing this BundleInfo into remove_bundle_info.

EntityCommands.retain just creates a new type Retain which runs EntityWorldMut.retain when run.


Changelog

Added EntityCommands.retain and EntityWorldMut.retain, which remove all components except the given bundle from the entity, they can also be used to remove all components by passing () as the bundle.

}

/// Removes any components in the [`Bundle`] from the entity.
// TODO: BundleRemover?
Copy link
Member

Choose a reason for hiding this comment

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

This TODO isn't clear to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know what this todo is referring to, it was already on remove so I left it there

Copy link
Contributor

@iiYese iiYese Dec 5, 2023

Choose a reason for hiding this comment

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

Going back through the commits looks like it's referring to a potential API to aggregate Bundle operations into builder APIs like a BundleRemover cause it also appears on other bundle operations.

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.

Great public docs, useful API, sensible implementation. Nice stuff.

Before merging, we should add some unit tests to verify that this continues to work as expected.

…`retain_nothing` checking clearing an entity (with `()`) and `retain_some_components` checking normal behaviour, including retaining a component that isn't on the entity.
@hymm hymm self-requested a review December 5, 2023 01:00
@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Dec 5, 2023
@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Dec 5, 2023
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Dec 5, 2023
Merged via the queue into bevyengine:main with commit 9da65b1 Dec 5, 2023
25 checks passed
@13ros27 13ros27 deleted the entity_retain branch December 5, 2023 18:47
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-Usability A targeted quality-of-life change that makes Bevy easier to use S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

API to retain bundle components on entities
3 participants