headless/headless.rs
1//! This example shows how to configure the `ScheduleRunnerPlugin` to run your
2//! application without windowing. You can completely remove rendering / windowing
3//! Plugin code from bevy by making your import look like this in your Cargo.toml.
4//!
5//! ```toml
6//! [dependencies]
7//! bevy = { version = "*", default-features = false }
8//! # replace "*" with the most recent version of bevy
9//! ```
10//!
11//! And then enabling the features you need.
12//! See the full list: <https://docs.rs/bevy/latest/bevy/#cargo-features>
13use bevy::{app::ScheduleRunnerPlugin, log::LogPlugin, prelude::*};
14use core::time::Duration;
15
16fn main() {
17 if cfg!(feature = "bevy_window") {
18 println!("This example is running with the bevy_window feature enabled and will not run headless.");
19 println!("Disable the default features and rerun the example to run headless.");
20 println!("To do so, run:");
21 println!();
22 println!(" cargo run --example headless --no-default-features --features bevy_log");
23 return;
24 }
25
26 // This app runs once
27 App::new()
28 .add_plugins(DefaultPlugins.set(ScheduleRunnerPlugin::run_once()))
29 .add_systems(Update, hello_world_system)
30 .run();
31
32 // This app loops forever at 60 fps
33 App::new()
34 .add_plugins(
35 DefaultPlugins
36 .set(ScheduleRunnerPlugin::run_loop(Duration::from_secs_f64(
37 1.0 / 60.0,
38 )))
39 // The log and ctrl+c plugin can only be registered once globally,
40 // which means we need to disable it here, because it was already registered with the
41 // app that runs once.
42 .disable::<LogPlugin>(),
43 )
44 .add_systems(Update, counter)
45 .run();
46}
47
48fn hello_world_system() {
49 println!("hello world");
50}
51
52fn counter(mut state: Local<CounterState>) {
53 if state.count.is_multiple_of(60) {
54 println!("{}", state.count);
55 }
56 state.count += 1;
57}
58
59#[derive(Default)]
60struct CounterState {
61 count: u32,
62}