Filter(-map) hiearchies when iterating descendants #21299
+361
−36
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Objective
When iterating over descendant entities: It can be a bit cumbersome to ignore recursive
RelationshipTargets
, when some condition on their parent (which we already visited during the iteration) has invalidated them for a given purpose. See more in the Showcase below.Solution
This PR adds 2 chain-able methods to entity descendant iteration (
iter_descendants
anditer_descendants_depth_first
) as a way to deal with this:filter_hierarchies
filter_map_hierarchies
Both allow skipping entire sub hierarchies based on a predicate/mapper applied their respective root entity. Breadth-first and depth-first traversal are abstracted behind a trait
DescendantsIteration
, so they could be nested in aFilterHierarchies
andFilterMapHierarchies
struct (in the spirit ofFilter<Iter<...>>
).Testing
test_iter_descendants
cargo test -p bevy_ecs test_iter_descendants
Showcase
The following example shows a system working around a
FontGroup
component. It applies a text fonts on itself and all children as long as those children are not managed by anotherFontGroup
component.