Skip to content

Commit 1f72e07

Browse files
djeedaimockersf
authored andcommitted
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 38dec27 commit 1f72e07

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
@@ -164,6 +164,35 @@ impl SubApp {
164164
self
165165
}
166166

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

0 commit comments

Comments
 (0)