Path: blob/main/crates/bevy_diagnostic/src/entity_count_diagnostics_plugin.rs
6595 views
use bevy_app::prelude::*;1use bevy_ecs::entity::Entities;23use crate::{4Diagnostic, DiagnosticPath, Diagnostics, RegisterDiagnostic, DEFAULT_MAX_HISTORY_LENGTH,5};67/// Adds "entity count" diagnostic to an App.8///9/// # See also10///11/// [`LogDiagnosticsPlugin`](crate::LogDiagnosticsPlugin) to output diagnostics to the console.12pub struct EntityCountDiagnosticsPlugin {13/// The total number of values to keep.14pub max_history_length: usize,15}1617impl Default for EntityCountDiagnosticsPlugin {18fn default() -> Self {19Self::new(DEFAULT_MAX_HISTORY_LENGTH)20}21}2223impl EntityCountDiagnosticsPlugin {24/// Creates a new `EntityCountDiagnosticsPlugin` with the specified `max_history_length`.25pub fn new(max_history_length: usize) -> Self {26Self { max_history_length }27}28}2930impl Plugin for EntityCountDiagnosticsPlugin {31fn build(&self, app: &mut App) {32app.register_diagnostic(33Diagnostic::new(Self::ENTITY_COUNT).with_max_history_length(self.max_history_length),34)35.add_systems(Update, Self::diagnostic_system);36}37}3839impl EntityCountDiagnosticsPlugin {40/// Number of currently allocated entities.41pub const ENTITY_COUNT: DiagnosticPath = DiagnosticPath::const_new("entity_count");4243/// Updates entity count measurement.44pub fn diagnostic_system(mut diagnostics: Diagnostics, entities: &Entities) {45diagnostics.add_measurement(&Self::ENTITY_COUNT, || entities.len() as f64);46}47}484950