Skip to content

Commit 1525dff

Browse files
authored
Add max_history_length to EntityCountDiagnosticsPlugin (#20085)
# Objective I was building out a diagnostics panel in egui when I noticed that I could specify the max history length for the `FrameTimeDiagnosticsPlugin`, but not for the `EntityCountDiagnosticsPlugin`. The objective was to harmonize the two, making the diagnostic history length configurable for both. ## Solution I added a `EntityCountDiagnosticsPlugin::new`, and a `Default` impl that matches `FrameTimeDiagnosticsPlugin`. This is a breaking change, given the plugin had no fields previously. ## Testing I did not test this.
1 parent 27fe2e8 commit 1525dff

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

crates/bevy_diagnostic/src/entity_count_diagnostics_plugin.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,39 @@
11
use bevy_app::prelude::*;
22
use bevy_ecs::entity::Entities;
33

4-
use crate::{Diagnostic, DiagnosticPath, Diagnostics, RegisterDiagnostic};
4+
use crate::{
5+
Diagnostic, DiagnosticPath, Diagnostics, RegisterDiagnostic, DEFAULT_MAX_HISTORY_LENGTH,
6+
};
57

68
/// Adds "entity count" diagnostic to an App.
79
///
810
/// # See also
911
///
1012
/// [`LogDiagnosticsPlugin`](crate::LogDiagnosticsPlugin) to output diagnostics to the console.
11-
#[derive(Default)]
12-
pub struct EntityCountDiagnosticsPlugin;
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+
}
1330

1431
impl Plugin for EntityCountDiagnosticsPlugin {
1532
fn build(&self, app: &mut App) {
16-
app.register_diagnostic(Diagnostic::new(Self::ENTITY_COUNT))
17-
.add_systems(Update, Self::diagnostic_system);
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);
1837
}
1938
}
2039

examples/diagnostics/log_diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn main() {
3333
// Adds frame time, FPS and frame count diagnostics.
3434
FrameTimeDiagnosticsPlugin::default(),
3535
// Adds an entity count diagnostic.
36-
EntityCountDiagnosticsPlugin,
36+
EntityCountDiagnosticsPlugin::default(),
3737
// Adds cpu and memory usage diagnostics for systems and the entire game process.
3838
SystemInformationDiagnosticsPlugin,
3939
// Forwards various diagnostics from the render app to the main app.

0 commit comments

Comments
 (0)