Skip to content

Commit f9c8f51

Browse files
authored
Add SubApp::take_extract() (#16862)
# Objective Fixes #16850 ## Solution Add a new function `SubApp::take_extract()`, similar to `Option::take()`, which allows stealing the currently installed extract function of a sub-app, with the intent to replace it with a custom one calling the original one via `set_extract()`. This pattern enables registering a custom "world sync" function similar to the existing one `entity_sync_system()`, to run custom world sync logic with mutable access to both the main and render worlds. ## Testing `cargo r -p ci` currently doesn't build locally, event after upgrading rustc to latest and doing a `cargo update`.
1 parent 43d5472 commit f9c8f51

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

crates/bevy_app/src/sub_app.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,35 @@ impl SubApp {
166166
self
167167
}
168168

169+
/// Take the function that will be called by [`extract`](Self::extract) out of the app, if any was set,
170+
/// and replace it with `None`.
171+
///
172+
/// If you use Bevy, `bevy_render` will set a default extract function used to extract data from
173+
/// the main world into the render world as part of the Extract phase. In that case, you cannot replace
174+
/// it with your own function. Instead, take the Bevy default function with this, and install your own
175+
/// instead which calls the Bevy default.
176+
///
177+
/// ```
178+
/// # use bevy_app::SubApp;
179+
/// # let mut app = SubApp::new();
180+
/// let default_fn = app.take_extract();
181+
/// app.set_extract(move |main, render| {
182+
/// // Do pre-extract custom logic
183+
/// // [...]
184+
///
185+
/// // Call Bevy's default, which executes the Extract phase
186+
/// if let Some(f) = default_fn.as_ref() {
187+
/// f(main, render);
188+
/// }
189+
///
190+
/// // Do post-extract custom logic
191+
/// // [...]
192+
/// });
193+
/// ```
194+
pub fn take_extract(&mut self) -> Option<ExtractFn> {
195+
self.extract.take()
196+
}
197+
169198
/// See [`App::insert_resource`].
170199
pub fn insert_resource<R: Resource>(&mut self, resource: R) -> &mut Self {
171200
self.world.insert_resource(resource);

0 commit comments

Comments
 (0)