From 01bdf67c33e631aa462fafc366ed6cae89906838 Mon Sep 17 00:00:00 2001 From: Kirillov Kirill Date: Thu, 7 Apr 2022 23:30:47 +0000 Subject: [PATCH] Improve the `set_active_camera` system (#4251) # Objective - Make `set_active_camera` system correctly respond to camera deletion, while preserving its correct behavior on first ever frame and any consequent frame, and with multiple cameras of the same type available in the world. - Fixes #4227 ## Solution - Add a check that the entity referred to by `ActiveCamera` still exists in the world. --- crates/bevy_render/src/camera/camera.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/crates/bevy_render/src/camera/camera.rs b/crates/bevy_render/src/camera/camera.rs index 528926ebd4..ca9f69f5cf 100644 --- a/crates/bevy_render/src/camera/camera.rs +++ b/crates/bevy_render/src/camera/camera.rs @@ -269,14 +269,22 @@ impl ActiveCamera { pub fn set_active_camera( mut active_camera: ResMut>, - cameras: Query>, + cameras: Query, With)>, ) { - if active_camera.get().is_some() { - return; + // Check if there is already an active camera set and + // that it has not been deleted on the previous frame + if let Some(camera) = active_camera.get() { + if cameras.contains(camera) { + return; + } } + // If the previous active camera ceased to exist + // fallback to another camera of the same type T if let Some(camera) = cameras.iter().next() { active_camera.camera = Some(camera); + } else { + active_camera.camera = None; } }