Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_diagnostic/src/entity_count_diagnostics_plugin.rs
6595 views
1
use bevy_app::prelude::*;
2
use bevy_ecs::entity::Entities;
3
4
use crate::{
5
Diagnostic, DiagnosticPath, Diagnostics, RegisterDiagnostic, DEFAULT_MAX_HISTORY_LENGTH,
6
};
7
8
/// Adds "entity count" diagnostic to an App.
9
///
10
/// # See also
11
///
12
/// [`LogDiagnosticsPlugin`](crate::LogDiagnosticsPlugin) to output diagnostics to the console.
13
pub struct EntityCountDiagnosticsPlugin {
14
/// The total number of values to keep.
15
pub max_history_length: usize,
16
}
17
18
impl Default for EntityCountDiagnosticsPlugin {
19
fn default() -> Self {
20
Self::new(DEFAULT_MAX_HISTORY_LENGTH)
21
}
22
}
23
24
impl EntityCountDiagnosticsPlugin {
25
/// Creates a new `EntityCountDiagnosticsPlugin` with the specified `max_history_length`.
26
pub fn new(max_history_length: usize) -> Self {
27
Self { max_history_length }
28
}
29
}
30
31
impl Plugin for EntityCountDiagnosticsPlugin {
32
fn build(&self, app: &mut App) {
33
app.register_diagnostic(
34
Diagnostic::new(Self::ENTITY_COUNT).with_max_history_length(self.max_history_length),
35
)
36
.add_systems(Update, Self::diagnostic_system);
37
}
38
}
39
40
impl EntityCountDiagnosticsPlugin {
41
/// Number of currently allocated entities.
42
pub const ENTITY_COUNT: DiagnosticPath = DiagnosticPath::const_new("entity_count");
43
44
/// Updates entity count measurement.
45
pub fn diagnostic_system(mut diagnostics: Diagnostics, entities: &Entities) {
46
diagnostics.add_measurement(&Self::ENTITY_COUNT, || entities.len() as f64);
47
}
48
}
49
50