Skip to content

Commit

Permalink
add setup function to app (bevyengine#7586)
Browse files Browse the repository at this point in the history
# Objective

- Fixes bevyengine#7412
- Fixes bevyengine#7576 

## Solution

- Add a setup function to app, so users can call the plugin `setup` methods before calling `update`.

## Changelog

- add a setup function to app
  • Loading branch information
hymm authored and myreprise1 committed Feb 11, 2023
1 parent ed5bf0d commit d12f9e3
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,17 +292,23 @@ impl App {
panic!("App::run() was called from within Plugin::Build(), which is not allowed.");
}

// temporarily remove the plugin registry to run each plugin's setup function on app.
let mut plugin_registry = std::mem::take(&mut app.plugin_registry);
for plugin in &plugin_registry {
plugin.setup(&mut app);
}
std::mem::swap(&mut app.plugin_registry, &mut plugin_registry);
Self::setup(&mut app);

let runner = std::mem::replace(&mut app.runner, Box::new(run_once));
(runner)(app);
}

/// Run [`Plugin::setup`] for each plugin. This is usually called by [`App::run`], but can
/// be useful for situations where you want to use [`App::update`].
pub fn setup(&mut self) {
// temporarily remove the plugin registry to run each plugin's setup function on app.
let plugin_registry = std::mem::take(&mut self.plugin_registry);
for plugin in &plugin_registry {
plugin.setup(self);
}
self.plugin_registry = plugin_registry;
}

/// Adds [`State<S>`] and [`NextState<S>`] resources, [`OnEnter`] and [`OnExit`] schedules
/// for each state variant, an instance of [`apply_state_transition::<S>`] in
/// [`CoreSet::StateTransitions`] so that transitions happen before [`CoreSet::Update`] and
Expand Down

0 comments on commit d12f9e3

Please sign in to comment.