Skip to content

Commit 9871e7e

Browse files
authored
Remove add_default_plugins and add MinimalPlugins for simple "headless" scenarios (bevyengine#767)
Remove add_default_plugins and add MinimalPlugins for simple "headless" scenarios
1 parent 2f87ff6 commit 9871e7e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+92
-80
lines changed

crates/bevy_app/src/schedule_runner.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,42 @@ impl Default for RunMode {
2929
}
3030
}
3131

32-
/// Configures an App to run its [Schedule](bevy_ecs::Schedule) according to a given [RunMode]
33-
#[derive(Default)]
34-
pub struct ScheduleRunnerPlugin {
32+
#[derive(Copy, Clone, Default)]
33+
pub struct ScheduleRunnerSettings {
3534
pub run_mode: RunMode,
3635
}
3736

38-
impl ScheduleRunnerPlugin {
37+
impl ScheduleRunnerSettings {
3938
pub fn run_once() -> Self {
40-
ScheduleRunnerPlugin {
39+
ScheduleRunnerSettings {
4140
run_mode: RunMode::Once,
4241
}
4342
}
4443

4544
pub fn run_loop(wait_duration: Duration) -> Self {
46-
ScheduleRunnerPlugin {
45+
ScheduleRunnerSettings {
4746
run_mode: RunMode::Loop {
4847
wait: Some(wait_duration),
4948
},
5049
}
5150
}
5251
}
5352

53+
/// Configures an App to run its [Schedule](bevy_ecs::Schedule) according to a given [RunMode]
54+
#[derive(Default)]
55+
pub struct ScheduleRunnerPlugin {}
56+
5457
impl Plugin for ScheduleRunnerPlugin {
5558
fn build(&self, app: &mut AppBuilder) {
56-
let run_mode = self.run_mode;
59+
let settings = app
60+
.resources_mut()
61+
.get_or_insert_with(ScheduleRunnerSettings::default)
62+
.to_owned();
5763
app.set_runner(move |mut app: App| {
5864
app.initialize();
5965

6066
let mut app_exit_event_reader = EventReader::<AppExit>::default();
61-
match run_mode {
67+
match settings.run_mode {
6268
RunMode::Once => {
6369
app.update();
6470
}

examples/2d/sprite.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use bevy::prelude::*;
22

33
fn main() {
44
App::build()
5-
.add_default_plugins()
5+
.add_plugin_group(DefaultPlugins)
66
.add_startup_system(setup.system())
77
.run();
88
}

examples/2d/sprite_sheet.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use bevy::prelude::*;
22

33
fn main() {
44
App::build()
5-
.add_default_plugins()
5+
.add_plugin_group(DefaultPlugins)
66
.add_startup_system(setup.system())
77
.add_system(animate_sprite_system.system())
88
.run();

examples/2d/texture_atlas.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use bevy::{asset::LoadState, prelude::*, sprite::TextureAtlasBuilder};
44
fn main() {
55
App::build()
66
.init_resource::<RpgSpriteHandles>()
7-
.add_default_plugins()
7+
.add_plugin_group(DefaultPlugins)
88
.add_startup_system(setup.system())
99
.add_system(load_atlas.system())
1010
.run();

examples/3d/3d_scene.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use bevy::prelude::*;
33
fn main() {
44
App::build()
55
.add_resource(Msaa { samples: 4 })
6-
.add_default_plugins()
6+
.add_plugin_group(DefaultPlugins)
77
.add_startup_system(setup.system())
88
.run();
99
}

examples/3d/load_gltf.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use bevy::prelude::*;
33
fn main() {
44
App::build()
55
.add_resource(Msaa { samples: 4 })
6-
.add_default_plugins()
6+
.add_plugin_group(DefaultPlugins)
77
.add_startup_system(setup.system())
88
.run();
99
}

examples/3d/msaa.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use bevy::prelude::*;
66
fn main() {
77
App::build()
88
.add_resource(Msaa { samples: 4 })
9-
.add_default_plugins()
9+
.add_plugin_group(DefaultPlugins)
1010
.add_startup_system(setup.system())
1111
.run();
1212
}

examples/3d/parenting.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use bevy::prelude::*;
55
fn main() {
66
App::build()
77
.add_resource(Msaa { samples: 4 })
8-
.add_default_plugins()
8+
.add_plugin_group(DefaultPlugins)
99
.add_startup_system(setup.system())
1010
.add_system(rotator_system.system())
1111
.run();

examples/3d/spawner.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rand::{rngs::StdRng, Rng, SeedableRng};
1010
/// NOTE: Bevy still has a number of optimizations to do in this area. Expect the performance here to go way up in the future
1111
fn main() {
1212
App::build()
13-
.add_default_plugins()
13+
.add_plugin_group(DefaultPlugins)
1414
.add_plugin(FrameTimeDiagnosticsPlugin::default())
1515
.add_plugin(PrintDiagnosticsPlugin::default())
1616
.add_startup_system(setup.system())

examples/3d/texture.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use bevy::prelude::*;
33
/// This example shows various ways to configure texture materials in 3D
44
fn main() {
55
App::build()
6-
.add_default_plugins()
6+
.add_plugin_group(DefaultPlugins)
77
.add_startup_system(setup.system())
88
.run();
99
}

examples/3d/z_sort_debug.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use bevy::{
99
/// This example visualizes camera z-ordering by setting the material of rotating cubes to their distance from the camera
1010
fn main() {
1111
App::build()
12-
.add_default_plugins()
12+
.add_plugin_group(DefaultPlugins)
1313
.add_startup_system(setup.system())
1414
.add_system(rotator_system.system())
1515
.add_system(camera_order_color_system.system())

examples/app/empty_defaults.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use bevy::prelude::*;
22

33
fn main() {
4-
App::build().add_default_plugins().run();
4+
App::build().add_plugin_group(DefaultPlugins).run();
55
}

examples/app/headless.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use bevy::{app::ScheduleRunnerPlugin, prelude::*};
1+
use bevy::{app::ScheduleRunnerSettings, prelude::*};
22
use std::time::Duration;
33

4-
// This example disables the default plugins by not registering them during setup.
4+
// This example only enables a minimal set of plugins required for bevy to run.
55
// You can also completely remove rendering / windowing Plugin code from bevy
66
// by making your import look like this in your Cargo.toml
77
//
@@ -12,15 +12,17 @@ use std::time::Duration;
1212
fn main() {
1313
// this app runs once
1414
App::build()
15-
.add_plugin(ScheduleRunnerPlugin::run_once())
15+
.add_resource(ScheduleRunnerSettings::run_once())
16+
.add_plugin_group(MinimalPlugins)
1617
.add_system(hello_world_system.system())
1718
.run();
1819

1920
// this app loops forever at 60 fps
2021
App::build()
21-
.add_plugin(ScheduleRunnerPlugin::run_loop(Duration::from_secs_f64(
22+
.add_resource(ScheduleRunnerSettings::run_loop(Duration::from_secs_f64(
2223
1.0 / 60.0,
2324
)))
25+
.add_plugin_group(MinimalPlugins)
2426
.add_system(counter.system())
2527
.run();
2628
}

examples/app/plugin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::time::Duration;
66
/// This example illustrates how to create a simple plugin that prints out a message.
77
fn main() {
88
App::build()
9-
.add_default_plugins()
9+
.add_plugin_group(DefaultPlugins)
1010
// plugins are registered as part of the "app building" process
1111
.add_plugin(PrintMessagePlugin {
1212
wait_duration: Duration::from_secs(1),

examples/app/plugin_group.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use bevy::{app::PluginGroupBuilder, prelude::*};
33
/// PluginGroups are a way to group sets of plugins that should be registered together.
44
fn main() {
55
App::build()
6-
// The app.add_default_plugins() you see in all of the examples is just an alias for this:
6+
// Two PluginGroups that are included with bevy are DefaultPlugins and MinimalPlugins
77
.add_plugin_group(DefaultPlugins)
88
// Adding a plugin group adds all plugins in the group by default
99
.add_plugin_group(HelloWorldPlugins)

examples/app/return_after_run.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ fn main() {
77
return_from_run: true,
88
})
99
.add_resource(ClearColor(Color::rgb(0.2, 0.2, 0.8)))
10-
.add_default_plugins()
10+
.add_plugin_group(DefaultPlugins)
1111
.run();
1212
println!("Running another App.");
1313
App::build()
1414
.add_resource(WinitConfig {
1515
return_from_run: true,
1616
})
1717
.add_resource(ClearColor(Color::rgb(0.2, 0.8, 0.2)))
18-
.add_default_plugins()
18+
.add_plugin_group(DefaultPlugins)
1919
.run();
2020
println!("Done.");
2121
}

examples/app/thread_pool_resources.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ use bevy::prelude::*;
55
fn main() {
66
App::build()
77
.add_resource(DefaultTaskPoolOptions::with_num_threads(4))
8-
.add_default_plugins()
8+
.add_plugin_group(DefaultPlugins)
99
.run();
1010
}

examples/asset/asset_loading.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use bevy::prelude::*;
44
fn main() {
55
App::build()
66
.add_resource(Msaa { samples: 4 })
7-
.add_default_plugins()
7+
.add_plugin_group(DefaultPlugins)
88
.add_startup_system(setup.system())
99
.run();
1010
}

examples/asset/custom_asset.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl AssetLoader for CustomAssetLoader {
3535

3636
fn main() {
3737
App::build()
38-
.add_default_plugins()
38+
.add_plugin_group(DefaultPlugins)
3939
.init_resource::<State>()
4040
.add_asset::<CustomAsset>()
4141
.init_asset_loader::<CustomAssetLoader>()

examples/asset/hot_asset_reloading.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use bevy::prelude::*;
55
/// This example illustrates hot reloading mesh changes.
66
fn main() {
77
App::build()
8-
.add_default_plugins()
8+
.add_plugin_group(DefaultPlugins)
99
.add_startup_system(setup.system())
1010
.run();
1111
}

examples/audio/audio.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use bevy::prelude::*;
33
/// This example illustrates how to load and play an audio file
44
fn main() {
55
App::build()
6-
.add_default_plugins()
6+
.add_plugin_group(DefaultPlugins)
77
.add_startup_system(setup.system())
88
.run();
99
}

examples/diagnostics/custom_diagnostic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use bevy::{
66
/// This example illustrates how to create a custom diagnostic
77
fn main() {
88
App::build()
9-
.add_default_plugins()
9+
.add_plugin_group(DefaultPlugins)
1010
// The "print diagnostics" plugin is optional. It just visualizes our diagnostics in the console
1111
.add_plugin(PrintDiagnosticsPlugin::default())
1212
.add_startup_system(setup_diagnostic_system.system())

examples/diagnostics/print_diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use bevy::{
55

66
fn main() {
77
App::build()
8-
.add_default_plugins()
8+
.add_plugin_group(DefaultPlugins)
99
// Adds frame time diagnostics
1010
.add_plugin(FrameTimeDiagnosticsPlugin::default())
1111
// Adds a system that prints diagnostics to the console

examples/ecs/ecs_guide.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use bevy::{
2-
app::{AppExit, ScheduleRunnerPlugin},
2+
app::{AppExit, ScheduleRunnerPlugin, ScheduleRunnerSettings},
33
prelude::*,
44
};
55
use rand::random;
@@ -244,12 +244,14 @@ fn local_state_system(mut state: Local<State>, query: Query<(&Player, &Score)>)
244244
fn main() {
245245
// Bevy apps are created using the builder pattern. We use the builder to add systems, resources, and plugins to our app
246246
App::build()
247-
// Plugins are just a grouped set of app builder calls (just like we're doing here).
248-
// We could easily turn our game into a plugin, but you can check out the plugin example for that :)
249-
// The plugin below runs our app's "system schedule" once every 5 seconds.
250-
.add_plugin(ScheduleRunnerPlugin::run_loop(Duration::from_secs(5)))
251247
// Resources can be added to our app like this
252248
.add_resource(State { counter: 0 })
249+
// Some systems are configured by adding their settings as a resource
250+
.add_resource(ScheduleRunnerSettings::run_loop(Duration::from_secs(5)))
251+
// Plugins are just a grouped set of app builder calls (just like we're doing here).
252+
// We could easily turn our game into a plugin, but you can check out the plugin example for that :)
253+
// The plugin below runs our app's "system schedule" once every 5 seconds (configured above).
254+
.add_plugin(ScheduleRunnerPlugin::default())
253255
// Resources that implement the Default or FromResources trait can be added like this:
254256
.init_resource::<GameState>()
255257
// Startup systems run exactly once BEFORE all other systems. These are generally used for

examples/ecs/event.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use bevy::prelude::*;
44
/// and a system that prints a message whenever the event is received.
55
fn main() {
66
App::build()
7-
.add_default_plugins()
7+
.add_plugin_group(DefaultPlugins)
88
.add_event::<MyEvent>()
99
.init_resource::<EventTriggerState>()
1010
.add_system(event_trigger_system.system())

examples/ecs/hierarchy.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use bevy::prelude::*;
22

33
fn main() {
44
App::build()
5-
.add_default_plugins()
5+
.add_plugin_group(DefaultPlugins)
66
.add_startup_system(setup.system())
77
.add_system(rotate.system())
88
.run();

examples/ecs/parallel_query.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ fn bounce_system(
7373

7474
fn main() {
7575
App::build()
76-
.add_default_plugins()
76+
.add_plugin_group(DefaultPlugins)
7777
.add_startup_system(spawn_system.system())
7878
.add_system(move_system.system())
7979
.add_system(bounce_system.system())

examples/game/breakout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use bevy::{
77
/// An implementation of the classic game "Breakout"
88
fn main() {
99
App::build()
10-
.add_default_plugins()
10+
.add_plugin_group(DefaultPlugins)
1111
.add_resource(Scoreboard { score: 0 })
1212
.add_resource(ClearColor(Color::rgb(0.9, 0.9, 0.9)))
1313
.add_startup_system(setup.system())

examples/input/gamepad_input.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use bevy_utils::HashSet;
44

55
fn main() {
66
App::build()
7-
.add_default_plugins()
7+
.add_plugin_group(DefaultPlugins)
88
.init_resource::<GamepadLobby>()
99
.add_system_to_stage(stage::PRE_UPDATE, connection_system.system())
1010
.add_system(gamepad_system.system())

examples/input/gamepad_input_events.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use bevy_input::gamepad::{GamepadEvent, GamepadEventType};
33

44
fn main() {
55
App::build()
6-
.add_default_plugins()
6+
.add_plugin_group(DefaultPlugins)
77
.add_system(gamepad_events.system())
88
.run();
99
}

examples/input/keyboard_input.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use bevy::{
55

66
fn main() {
77
App::build()
8-
.add_default_plugins()
8+
.add_plugin_group(DefaultPlugins)
99
.add_system(keyboard_input_system.system())
1010
.run();
1111
}

examples/input/keyboard_input_events.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use bevy::{input::keyboard::KeyboardInput, prelude::*};
22

33
fn main() {
44
App::build()
5-
.add_default_plugins()
5+
.add_plugin_group(DefaultPlugins)
66
.add_system(print_keyboard_event_system.system())
77
.run();
88
}

examples/input/mouse_input.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use bevy::prelude::*;
22

33
fn main() {
44
App::build()
5-
.add_default_plugins()
5+
.add_plugin_group(DefaultPlugins)
66
.add_system(mouse_click_system.system())
77
.run();
88
}

examples/input/mouse_input_events.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use bevy::{
66

77
fn main() {
88
App::build()
9-
.add_default_plugins()
9+
.add_plugin_group(DefaultPlugins)
1010
.add_system(print_mouse_events_system.system())
1111
.run();
1212
}

0 commit comments

Comments
 (0)