From b22db47e1003db2d712baec46c0f0a1f88b0a837 Mon Sep 17 00:00:00 2001 From: Raffaele Ragni Date: Fri, 27 Oct 2023 05:59:29 +0200 Subject: [PATCH] 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. --- crates/bevy_render/src/view/visibility/mod.rs | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/crates/bevy_render/src/view/visibility/mod.rs b/crates/bevy_render/src/view/visibility/mod.rs index b018103afc6c5..2a465c55273e5 100644 --- a/crates/bevy_render/src/view/visibility/mod.rs +++ b/crates/bevy_render/src/view/visibility/mod.rs @@ -310,10 +310,10 @@ fn visibility_propagate_system( let is_visible = match visibility { Visibility::Visible => true, Visibility::Hidden => false, - Visibility::Inherited => match parent { - None => true, - Some(parent) => visibility_query.get(parent.get()).unwrap().1.get(), - }, + // fall back to true if no parent is found or parent lacks components + Visibility::Inherited => parent + .and_then(|p| visibility_query.get(p.get()).ok()) + .map_or(true, |(_, x)| x.get()), }; let (_, mut inherited_visibility) = visibility_query .get_mut(entity) @@ -721,6 +721,24 @@ mod test { 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::().unwrap().0; + // defaults to same behavior of parent not found: visible = true + assert!(child_visible); + } + #[test] fn ensure_visibility_enum_size() { use std::mem;