bevy/examples/diagnostics/custom_diagnostic.rs
LeshaInc 320ac65a9e
Replace DiagnosticId by DiagnosticPath (#9266)
# Objective

Implements #9216 

## Solution

- Replace `DiagnosticId` by `DiagnosticPath`. It's pre-hashed using
`const-fnv1a-hash` crate, so it's possible to create path in const
contexts.

---

## Changelog

- Replaced `DiagnosticId` by `DiagnosticPath`
- Set default history length to 120 measurements (2 seconds on 60 fps).

I've noticed hardcoded constant 20 everywhere and decided to change it
to `DEFAULT_MAX_HISTORY_LENGTH` , which is set to new diagnostics by
default. To override it, use `with_max_history_length`.


## Migration Guide

```diff
- const UNIQUE_DIAG_ID: DiagnosticId = DiagnosticId::from_u128(42);
+ const UNIQUE_DIAG_PATH: DiagnosticPath = DiagnosticPath::const_new("foo/bar");

- Diagnostic::new(UNIQUE_DIAG_ID, "example", 10)
+ Diagnostic::new(UNIQUE_DIAG_PATH).with_max_history_length(10)

- diagnostics.add_measurement(UNIQUE_DIAG_ID, || 42);
+ diagnostics.add_measurement(&UNIQUE_DIAG_ID, || 42);
```
2024-01-20 15:42:51 +00:00

31 lines
1 KiB
Rust

//! This example illustrates how to create a custom diagnostic.
use bevy::{
diagnostic::{
Diagnostic, DiagnosticPath, Diagnostics, LogDiagnosticsPlugin, RegisterDiagnostic,
},
prelude::*,
};
fn main() {
App::new()
.add_plugins((
DefaultPlugins,
// The "print diagnostics" plugin is optional.
// It just visualizes our diagnostics in the console.
LogDiagnosticsPlugin::default(),
))
// Diagnostics must be initialized before measurements can be added.
.register_diagnostic(Diagnostic::new(SYSTEM_ITERATION_COUNT).with_suffix(" iterations"))
.add_systems(Update, my_system)
.run();
}
// All diagnostics should have a unique DiagnosticPath.
pub const SYSTEM_ITERATION_COUNT: DiagnosticPath =
DiagnosticPath::const_new("system_iteration_count");
fn my_system(mut diagnostics: Diagnostics) {
// Add a measurement of 10.0 for our diagnostic each time this system runs.
diagnostics.add_measurement(&SYSTEM_ITERATION_COUNT, || 10.0);
}