Skip to content

Convenient way to add minimal plugins for a headless server #767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions crates/bevy_app/src/schedule_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,42 @@ impl Default for RunMode {
}
}

/// Configures an App to run its [Schedule](bevy_ecs::Schedule) according to a given [RunMode]
#[derive(Default)]
pub struct ScheduleRunnerPlugin {
#[derive(Copy, Clone, Default)]
pub struct ScheduleRunnerSettings {
pub run_mode: RunMode,
}

impl ScheduleRunnerPlugin {
impl ScheduleRunnerSettings {
pub fn run_once() -> Self {
ScheduleRunnerPlugin {
ScheduleRunnerSettings {
run_mode: RunMode::Once,
}
}

pub fn run_loop(wait_duration: Duration) -> Self {
ScheduleRunnerPlugin {
ScheduleRunnerSettings {
run_mode: RunMode::Loop {
wait: Some(wait_duration),
},
}
}
}

/// Configures an App to run its [Schedule](bevy_ecs::Schedule) according to a given [RunMode]
#[derive(Default)]
pub struct ScheduleRunnerPlugin {}

impl Plugin for ScheduleRunnerPlugin {
fn build(&self, app: &mut AppBuilder) {
let run_mode = self.run_mode;
let settings = app
.resources_mut()
.get_or_insert_with(ScheduleRunnerSettings::default)
.to_owned();
app.set_runner(move |mut app: App| {
app.initialize();

let mut app_exit_event_reader = EventReader::<AppExit>::default();
match run_mode {
match settings.run_mode {
RunMode::Once => {
app.update();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/2d/sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bevy::prelude::*;

fn main() {
App::build()
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.add_startup_system(setup.system())
.run();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/2d/sprite_sheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bevy::prelude::*;

fn main() {
App::build()
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.add_startup_system(setup.system())
.add_system(animate_sprite_system.system())
.run();
Expand Down
2 changes: 1 addition & 1 deletion examples/2d/texture_atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bevy::{asset::LoadState, prelude::*, sprite::TextureAtlasBuilder};
fn main() {
App::build()
.init_resource::<RpgSpriteHandles>()
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.add_startup_system(setup.system())
.add_system(load_atlas.system())
.run();
Expand Down
2 changes: 1 addition & 1 deletion examples/3d/3d_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bevy::prelude::*;
fn main() {
App::build()
.add_resource(Msaa { samples: 4 })
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.add_startup_system(setup.system())
.run();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/3d/load_gltf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bevy::prelude::*;
fn main() {
App::build()
.add_resource(Msaa { samples: 4 })
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.add_startup_system(setup.system())
.run();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/3d/msaa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bevy::prelude::*;
fn main() {
App::build()
.add_resource(Msaa { samples: 4 })
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.add_startup_system(setup.system())
.run();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/3d/parenting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bevy::prelude::*;
fn main() {
App::build()
.add_resource(Msaa { samples: 4 })
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.add_startup_system(setup.system())
.add_system(rotator_system.system())
.run();
Expand Down
2 changes: 1 addition & 1 deletion examples/3d/spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rand::{rngs::StdRng, Rng, SeedableRng};
/// NOTE: Bevy still has a number of optimizations to do in this area. Expect the performance here to go way up in the future
fn main() {
App::build()
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.add_plugin(FrameTimeDiagnosticsPlugin::default())
.add_plugin(PrintDiagnosticsPlugin::default())
.add_startup_system(setup.system())
Expand Down
2 changes: 1 addition & 1 deletion examples/3d/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bevy::prelude::*;
/// This example shows various ways to configure texture materials in 3D
fn main() {
App::build()
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.add_startup_system(setup.system())
.run();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/3d/z_sort_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use bevy::{
/// This example visualizes camera z-ordering by setting the material of rotating cubes to their distance from the camera
fn main() {
App::build()
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.add_startup_system(setup.system())
.add_system(rotator_system.system())
.add_system(camera_order_color_system.system())
Expand Down
2 changes: 1 addition & 1 deletion examples/app/empty_defaults.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::prelude::*;

fn main() {
App::build().add_default_plugins().run();
App::build().add_plugin_group(DefaultPlugins).run();
}
10 changes: 6 additions & 4 deletions examples/app/headless.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bevy::{app::ScheduleRunnerPlugin, prelude::*};
use bevy::{app::ScheduleRunnerSettings, prelude::*};
use std::time::Duration;

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

// this app loops forever at 60 fps
App::build()
.add_plugin(ScheduleRunnerPlugin::run_loop(Duration::from_secs_f64(
.add_resource(ScheduleRunnerSettings::run_loop(Duration::from_secs_f64(
1.0 / 60.0,
)))
.add_plugin_group(MinimalPlugins)
.add_system(counter.system())
.run();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/app/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::time::Duration;
/// This example illustrates how to create a simple plugin that prints out a message.
fn main() {
App::build()
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
// plugins are registered as part of the "app building" process
.add_plugin(PrintMessagePlugin {
wait_duration: Duration::from_secs(1),
Expand Down
2 changes: 1 addition & 1 deletion examples/app/plugin_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bevy::{app::PluginGroupBuilder, prelude::*};
/// PluginGroups are a way to group sets of plugins that should be registered together.
fn main() {
App::build()
// The app.add_default_plugins() you see in all of the examples is just an alias for this:
// Two PluginGroups that are included with bevy are DefaultPlugins and MinimalPlugins
.add_plugin_group(DefaultPlugins)
// Adding a plugin group adds all plugins in the group by default
.add_plugin_group(HelloWorldPlugins)
Expand Down
4 changes: 2 additions & 2 deletions examples/app/return_after_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ fn main() {
return_from_run: true,
})
.add_resource(ClearColor(Color::rgb(0.2, 0.2, 0.8)))
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.run();
println!("Running another App.");
App::build()
.add_resource(WinitConfig {
return_from_run: true,
})
.add_resource(ClearColor(Color::rgb(0.2, 0.8, 0.2)))
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.run();
println!("Done.");
}
2 changes: 1 addition & 1 deletion examples/app/thread_pool_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ use bevy::prelude::*;
fn main() {
App::build()
.add_resource(DefaultTaskPoolOptions::with_num_threads(4))
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.run();
}
2 changes: 1 addition & 1 deletion examples/asset/asset_loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bevy::prelude::*;
fn main() {
App::build()
.add_resource(Msaa { samples: 4 })
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.add_startup_system(setup.system())
.run();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/asset/custom_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl AssetLoader for CustomAssetLoader {

fn main() {
App::build()
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.init_resource::<State>()
.add_asset::<CustomAsset>()
.init_asset_loader::<CustomAssetLoader>()
Expand Down
2 changes: 1 addition & 1 deletion examples/asset/hot_asset_reloading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bevy::prelude::*;
/// This example illustrates hot reloading mesh changes.
fn main() {
App::build()
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.add_startup_system(setup.system())
.run();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/audio/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bevy::prelude::*;
/// This example illustrates how to load and play an audio file
fn main() {
App::build()
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.add_startup_system(setup.system())
.run();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/diagnostics/custom_diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bevy::{
/// This example illustrates how to create a custom diagnostic
fn main() {
App::build()
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
// The "print diagnostics" plugin is optional. It just visualizes our diagnostics in the console
.add_plugin(PrintDiagnosticsPlugin::default())
.add_startup_system(setup_diagnostic_system.system())
Expand Down
2 changes: 1 addition & 1 deletion examples/diagnostics/print_diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bevy::{

fn main() {
App::build()
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
// Adds frame time diagnostics
.add_plugin(FrameTimeDiagnosticsPlugin::default())
// Adds a system that prints diagnostics to the console
Expand Down
12 changes: 7 additions & 5 deletions examples/ecs/ecs_guide.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::{
app::{AppExit, ScheduleRunnerPlugin},
app::{AppExit, ScheduleRunnerPlugin, ScheduleRunnerSettings},
prelude::*,
};
use rand::random;
Expand Down Expand Up @@ -244,12 +244,14 @@ fn local_state_system(mut state: Local<State>, query: Query<(&Player, &Score)>)
fn main() {
// Bevy apps are created using the builder pattern. We use the builder to add systems, resources, and plugins to our app
App::build()
// Plugins are just a grouped set of app builder calls (just like we're doing here).
// We could easily turn our game into a plugin, but you can check out the plugin example for that :)
// The plugin below runs our app's "system schedule" once every 5 seconds.
.add_plugin(ScheduleRunnerPlugin::run_loop(Duration::from_secs(5)))
// Resources can be added to our app like this
.add_resource(State { counter: 0 })
// Some systems are configured by adding their settings as a resource
.add_resource(ScheduleRunnerSettings::run_loop(Duration::from_secs(5)))
// Plugins are just a grouped set of app builder calls (just like we're doing here).
// We could easily turn our game into a plugin, but you can check out the plugin example for that :)
// The plugin below runs our app's "system schedule" once every 5 seconds (configured above).
.add_plugin(ScheduleRunnerPlugin::default())
// Resources that implement the Default or FromResources trait can be added like this:
.init_resource::<GameState>()
// Startup systems run exactly once BEFORE all other systems. These are generally used for
Expand Down
2 changes: 1 addition & 1 deletion examples/ecs/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bevy::prelude::*;
/// and a system that prints a message whenever the event is received.
fn main() {
App::build()
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.add_event::<MyEvent>()
.init_resource::<EventTriggerState>()
.add_system(event_trigger_system.system())
Expand Down
2 changes: 1 addition & 1 deletion examples/ecs/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bevy::prelude::*;

fn main() {
App::build()
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.add_startup_system(setup.system())
.add_system(rotate.system())
.run();
Expand Down
2 changes: 1 addition & 1 deletion examples/ecs/parallel_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn bounce_system(

fn main() {
App::build()
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.add_startup_system(spawn_system.system())
.add_system(move_system.system())
.add_system(bounce_system.system())
Expand Down
2 changes: 1 addition & 1 deletion examples/game/breakout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bevy::{
/// An implementation of the classic game "Breakout"
fn main() {
App::build()
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.add_resource(Scoreboard { score: 0 })
.add_resource(ClearColor(Color::rgb(0.9, 0.9, 0.9)))
.add_startup_system(setup.system())
Expand Down
2 changes: 1 addition & 1 deletion examples/input/gamepad_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bevy_utils::HashSet;

fn main() {
App::build()
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.init_resource::<GamepadLobby>()
.add_system_to_stage(stage::PRE_UPDATE, connection_system.system())
.add_system(gamepad_system.system())
Expand Down
2 changes: 1 addition & 1 deletion examples/input/gamepad_input_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bevy_input::gamepad::{GamepadEvent, GamepadEventType};

fn main() {
App::build()
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.add_system(gamepad_events.system())
.run();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/input/keyboard_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bevy::{

fn main() {
App::build()
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.add_system(keyboard_input_system.system())
.run();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/input/keyboard_input_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bevy::{input::keyboard::KeyboardInput, prelude::*};

fn main() {
App::build()
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.add_system(print_keyboard_event_system.system())
.run();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/input/mouse_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bevy::prelude::*;

fn main() {
App::build()
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.add_system(mouse_click_system.system())
.run();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/input/mouse_input_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bevy::{

fn main() {
App::build()
.add_default_plugins()
.add_plugin_group(DefaultPlugins)
.add_system(print_mouse_events_system.system())
.run();
}
Expand Down
Loading