Skip to content

Commit 937fc03

Browse files
committed
Convenience method for entity naming (#7186)
# Objective - Trying to make it easier to have a more user friendly debugging name for when you want to print out an entity. ## Solution - Add a new `WorldQuery` struct `DebugName` to format the `Name` if the entity has one, otherwise formats the `Entity` id. This means we can do this and get more descriptive errors without much more effort: ```rust fn my_system(moving: Query<(DebugName, &mut Position, &Velocity)>) { for (name, mut position, velocity) in &mut moving { position += velocity; if position.is_nan() { error!("{:?} has an invalid position state", name); } } } ``` --- ## Changelog - Added `DebugName` world query for more human friendly debug names of entities.
1 parent ca2d91e commit 937fc03

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

crates/bevy_core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub mod prelude {
1515
//! The Bevy Core Prelude.
1616
#[doc(hidden)]
1717
pub use crate::{
18-
FrameCountPlugin, Name, TaskPoolOptions, TaskPoolPlugin, TypeRegistrationPlugin,
18+
DebugName, FrameCountPlugin, Name, TaskPoolOptions, TaskPoolPlugin, TypeRegistrationPlugin,
1919
};
2020
}
2121

crates/bevy_core/src/name.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use bevy_ecs::{component::Component, reflect::ReflectComponent};
1+
use bevy_ecs::{
2+
component::Component, entity::Entity, query::WorldQuery, reflect::ReflectComponent,
3+
};
24
use bevy_reflect::Reflect;
35
use bevy_reflect::{std_traits::ReflectDefault, FromReflect};
46
use bevy_utils::AHasher;
@@ -77,6 +79,40 @@ impl std::fmt::Display for Name {
7779
}
7880
}
7981

82+
/// Convenient query for giving a human friendly name to an entity.
83+
///
84+
/// ```rust
85+
/// # use bevy_core::prelude::*;
86+
/// # use bevy_ecs::prelude::*;
87+
/// # #[derive(Component)] pub struct Score(f32);
88+
/// fn increment_score(mut scores: Query<(DebugName, &mut Score)>) {
89+
/// for (name, mut score) in &mut scores {
90+
/// score.0 += 1.0;
91+
/// if score.0.is_nan() {
92+
/// bevy_utils::tracing::error!("Score for {:?} is invalid", name);
93+
/// }
94+
/// }
95+
/// }
96+
/// # bevy_ecs::system::assert_is_system(increment_score);
97+
/// ```
98+
#[derive(WorldQuery)]
99+
pub struct DebugName {
100+
/// A [`Name`] that the entity might have that is displayed if available.
101+
pub name: Option<&'static Name>,
102+
/// The unique identifier of the entity as a fallback.
103+
pub entity: Entity,
104+
}
105+
106+
impl<'a> std::fmt::Debug for DebugNameItem<'a> {
107+
#[inline(always)]
108+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
109+
match self.name {
110+
Some(name) => write!(f, "{:?} ({:?})", &name, &self.entity),
111+
None => std::fmt::Debug::fmt(&self.entity, f),
112+
}
113+
}
114+
}
115+
80116
/* Conversions from strings */
81117

82118
impl From<&str> for Name {

0 commit comments

Comments
 (0)