Initialize DiagnosticStore on register_diagnostic if it does not exist (#8819)

# Objective

- Fixes #8782

## Solution

- Call `init_resource` on `register_diagnostic` so that a
`DiagnosticStore` gets initialized if it does not exist.

---------

Co-authored-by: François <mockersf@gmail.com>
This commit is contained in:
Opstic 2023-06-12 15:40:09 -04:00 committed by GitHub
parent 1977b6daf2
commit 2b4fc10ccf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -289,6 +289,8 @@ pub trait RegisterDiagnostic {
impl RegisterDiagnostic for App {
/// Register a new [`Diagnostic`] with an [`App`].
///
/// Will initialize a [`DiagnosticsStore`] if it doesn't exist.
///
/// ```rust
/// use bevy_app::App;
/// use bevy_diagnostic::{Diagnostic, DiagnosticsPlugin, DiagnosticId, RegisterDiagnostic};
@ -296,12 +298,12 @@ impl RegisterDiagnostic for App {
/// const UNIQUE_DIAG_ID: DiagnosticId = DiagnosticId::from_u128(42);
///
/// App::new()
/// .add_plugin(DiagnosticsPlugin)
/// // Must only be called after the `DiagnosticsPlugin` has been added.
/// .register_diagnostic(Diagnostic::new(UNIQUE_DIAG_ID, "example", 10))
/// .add_plugin(DiagnosticsPlugin)
/// .run();
/// ```
fn register_diagnostic(&mut self, diagnostic: Diagnostic) -> &mut Self {
self.init_resource::<DiagnosticsStore>();
let mut diagnostics = self.world.resource_mut::<DiagnosticsStore>();
diagnostics.add(diagnostic);