Skip to content

Commit

Permalink
Document diagnostic module
Browse files Browse the repository at this point in the history
  • Loading branch information
manokara committed Jan 10, 2022
1 parent 5f5e212 commit d9282bb
Showing 1 changed file with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bevy_app::prelude::*;
use bevy_diagnostic::{Diagnostic, DiagnosticId, Diagnostics, MAX_DIAGNOSTIC_NAME_WIDTH};
use bevy_ecs::system::{Res, ResMut};

/// Adds "asset count" diagnostic to an App
/// Adds an asset count diagnostic to an App`] for assets of type `T`.
pub struct AssetCountDiagnosticsPlugin<T: Asset> {
marker: std::marker::PhantomData<T>,
}
Expand All @@ -24,10 +24,14 @@ impl<T: Asset> Plugin for AssetCountDiagnosticsPlugin<T> {
}

impl<T: Asset> AssetCountDiagnosticsPlugin<T> {
/// Gets unique id of this diagnostic.
///
/// The diagnostic id is the type uuid of `T`.
pub fn diagnostic_id() -> DiagnosticId {
DiagnosticId(T::TYPE_UUID)
}

/// Registers the asset count diagnostic for the current application.
pub fn setup_system(mut diagnostics: ResMut<Diagnostics>) {
let asset_type_name = std::any::type_name::<T>();
let max_length = MAX_DIAGNOSTIC_NAME_WIDTH - "asset_count ".len();
Expand All @@ -47,6 +51,7 @@ impl<T: Asset> AssetCountDiagnosticsPlugin<T> {
));
}

/// Updates the asset count of `T` assets.
pub fn diagnostic_system(mut diagnostics: ResMut<Diagnostics>, assets: Res<Assets<T>>) {
diagnostics.add_measurement(Self::diagnostic_id(), assets.len() as f64);
}
Expand Down

0 comments on commit d9282bb

Please sign in to comment.