Skip to content

Commit f51a306

Browse files
feat(bevy_app): expose an API to perform updates for a specific sub-app. (#14009)
# Objective - Fixes #14003 ## Solution - Expose an API to perform updates for a specific sub-app, so we can avoid mutable borrow the app twice. ## Testing - I have tested the API by modifying the code in the `many_lights` example with the following changes: ```rust impl Plugin for LogVisibleLights { fn build(&self, app: &mut App) { let Some(render_app) = app.get_sub_app_mut(RenderApp) else { return; }; render_app.add_systems(Render, print_visible_light_count.in_set(RenderSet::Prepare)); } fn finish(&self, app: &mut App) { app.update_sub_app_by_label(RenderApp); } } ``` --- ## Changelog - add the `update_sub_app_by_label` API to `App` and `SubApps`. --------- Co-authored-by: Jan Hohenheim <[email protected]>
1 parent 1df811e commit f51a306

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

crates/bevy_app/src/app.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,11 @@ impl App {
663663
self.sub_apps.sub_apps.remove(&label.intern())
664664
}
665665

666+
/// Extract data from the main world into the [`SubApp`] with the given label and perform an update if it exists.
667+
pub fn update_sub_app_by_label(&mut self, label: impl AppLabel) {
668+
self.sub_apps.update_subapp_by_label(label);
669+
}
670+
666671
/// Inserts a new `schedule` under the provided `label`, overwriting any existing
667672
/// schedule with the same label.
668673
pub fn add_schedule(&mut self, schedule: Schedule) -> &mut Self {

crates/bevy_app/src/sub_app.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{App, InternedAppLabel, Plugin, Plugins, PluginsState};
1+
use crate::{App, AppLabel, InternedAppLabel, Plugin, Plugins, PluginsState};
22
use bevy_ecs::{
33
event::EventRegistry,
44
prelude::*,
@@ -449,4 +449,12 @@ impl SubApps {
449449
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut SubApp> + '_ {
450450
std::iter::once(&mut self.main).chain(self.sub_apps.values_mut())
451451
}
452+
453+
/// Extract data from the main world into the [`SubApp`] with the given label and perform an update if it exists.
454+
pub fn update_subapp_by_label(&mut self, label: impl AppLabel) {
455+
if let Some(sub_app) = self.sub_apps.get_mut(&label.intern()) {
456+
sub_app.extract(&mut self.main.world);
457+
sub_app.update();
458+
}
459+
}
452460
}

0 commit comments

Comments
 (0)