Skip to content

Commit

Permalink
default inherited visibility when parent has invalid components (bevy…
Browse files Browse the repository at this point in the history
…engine#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.
  • Loading branch information
raffaeleragni authored Oct 27, 2023
1 parent cfcc113 commit b22db47
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions crates/bevy_render/src/view/visibility/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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::<InheritedVisibility>().unwrap().0;
// defaults to same behavior of parent not found: visible = true
assert!(child_visible);
}

#[test]
fn ensure_visibility_enum_size() {
use std::mem;
Expand Down

0 comments on commit b22db47

Please sign in to comment.