plugin/
plugin.rs

1//! Demonstrates the creation and registration of a custom plugin.
2//!
3//! Plugins are the foundation of Bevy. They are scoped sets of components, resources, and systems
4//! that provide a specific piece of functionality (generally the smaller the scope, the better).
5//! This example illustrates how to create a simple plugin that prints out a message.
6
7use bevy::prelude::*;
8use core::time::Duration;
9
10fn main() {
11    App::new()
12        // plugins are registered as part of the "app building" process
13        .add_plugins((
14            DefaultPlugins,
15            PrintMessagePlugin {
16                wait_duration: Duration::from_secs(1),
17                message: "This is an example plugin".to_string(),
18            },
19        ))
20        .run();
21}
22
23// This "print message plugin" prints a `message` every `wait_duration`
24struct PrintMessagePlugin {
25    // Put your plugin configuration here
26    wait_duration: Duration,
27    message: String,
28}
29
30impl Plugin for PrintMessagePlugin {
31    // this is where we set up our plugin
32    fn build(&self, app: &mut App) {
33        let state = PrintMessageState {
34            message: self.message.clone(),
35            timer: Timer::new(self.wait_duration, TimerMode::Repeating),
36        };
37        app.insert_resource(state)
38            .add_systems(Update, print_message_system);
39    }
40}
41
42#[derive(Resource)]
43struct PrintMessageState {
44    message: String,
45    timer: Timer,
46}
47
48fn print_message_system(mut state: ResMut<PrintMessageState>, time: Res<Time>) {
49    if state.timer.tick(time.delta()).finished() {
50        info!("{}", state.message);
51    }
52}