docs/bevy/examples/diagnostics/custom_diagnostic.rs 1 K · 30 lines · raw · history

1 //! This example illustrates how to create a custom diagnostic.
2
3 use bevy::{
4 diagnostic::{
5 Diagnostic, DiagnosticPath, Diagnostics, LogDiagnosticsPlugin, RegisterDiagnostic,
6 },
7 prelude::*,
8 };
9
10 fn main() {
11 App::new()
12 .add_plugins((
13 DefaultPlugins,
14 // The "print diagnostics" plugin is optional.
15 // It just visualizes our diagnostics in the console.
16 LogDiagnosticsPlugin::default(),
17 ))
18 // Diagnostics must be initialized before measurements can be added.
19 .register_diagnostic(Diagnostic::new(SYSTEM_ITERATION_COUNT).with_suffix(" iterations"))
20 .add_systems(Update, my_system)
21 .run();
22 }
23
24 // All diagnostics should have a unique DiagnosticPath.
25 const SYSTEM_ITERATION_COUNT: DiagnosticPath = DiagnosticPath::const_new("system_iteration_count");
26
27 fn my_system(mut diagnostics: Diagnostics) {
28 // Add a measurement of 10.0 for our diagnostic each time this system runs.
29 diagnostics.add_measurement(&SYSTEM_ITERATION_COUNT, || 10.0);
30 }