|
1 |
| -use bevy_ecs::{component::Component, reflect::ReflectComponent}; |
| 1 | +use bevy_ecs::{ |
| 2 | + component::Component, entity::Entity, query::WorldQuery, reflect::ReflectComponent, |
| 3 | +}; |
2 | 4 | use bevy_reflect::Reflect;
|
3 | 5 | use bevy_reflect::{std_traits::ReflectDefault, FromReflect};
|
4 | 6 | use bevy_utils::AHasher;
|
@@ -77,6 +79,40 @@ impl std::fmt::Display for Name {
|
77 | 79 | }
|
78 | 80 | }
|
79 | 81 |
|
| 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 | + |
80 | 116 | /* Conversions from strings */
|
81 | 117 |
|
82 | 118 | impl From<&str> for Name {
|
|
0 commit comments