default inherited visibility when parent has invalid components (#10275)

# Situation

- In case of parent without visibility components, the visibility
inheritance of children creates a panic.

## Solution

- Apply same fallback visibility as parent not found instead of panic.
This commit is contained in:
Raffaele Ragni 2023-10-27 05:59:29 +02:00 committed by GitHub
parent cfcc113fb7
commit b22db47e10
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -310,10 +310,10 @@ fn visibility_propagate_system(
let is_visible = match visibility { let is_visible = match visibility {
Visibility::Visible => true, Visibility::Visible => true,
Visibility::Hidden => false, Visibility::Hidden => false,
Visibility::Inherited => match parent { // fall back to true if no parent is found or parent lacks components
None => true, Visibility::Inherited => parent
Some(parent) => visibility_query.get(parent.get()).unwrap().1.get(), .and_then(|p| visibility_query.get(p.get()).ok())
}, .map_or(true, |(_, x)| x.get()),
}; };
let (_, mut inherited_visibility) = visibility_query let (_, mut inherited_visibility) = visibility_query
.get_mut(entity) .get_mut(entity)
@ -721,6 +721,24 @@ mod test {
assert!(!q.get(&world, id4).unwrap().is_changed()); assert!(!q.get(&world, id4).unwrap().is_changed());
} }
#[test]
fn visibility_propagation_with_invalid_parent() {
let mut world = World::new();
let mut schedule = Schedule::default();
schedule.add_systems(visibility_propagate_system);
let parent = world.spawn(()).id();
let child = world.spawn(VisibilityBundle::default()).id();
world.entity_mut(parent).push_children(&[child]);
schedule.run(&mut world);
world.clear_trackers();
let child_visible = world.entity(child).get::<InheritedVisibility>().unwrap().0;
// defaults to same behavior of parent not found: visible = true
assert!(child_visible);
}
#[test] #[test]
fn ensure_visibility_enum_size() { fn ensure_visibility_enum_size() {
use std::mem; use std::mem;