Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/app/plugin.rs
6593 views
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
7
use bevy::prelude::*;
8
use core::time::Duration;
9
10
fn 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`
24
struct PrintMessagePlugin {
25
// Put your plugin configuration here
26
wait_duration: Duration,
27
message: String,
28
}
29
30
impl 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)]
43
struct PrintMessageState {
44
message: String,
45
timer: Timer,
46
}
47
48
fn print_message_system(mut state: ResMut<PrintMessageState>, time: Res<Time>) {
49
if state.timer.tick(time.delta()).is_finished() {
50
info!("{}", state.message);
51
}
52
}
53
54