Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Convenience method for entity naming #7186

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/bevy_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ pub mod prelude {
//! The Bevy Core Prelude.
#[doc(hidden)]
pub use crate::{
FrameCountPlugin, Name, TaskPoolOptions, TaskPoolPlugin, TypeRegistrationPlugin,
DebugNameQueryExt, FrameCountPlugin, Name, TaskPoolOptions, TaskPoolPlugin,
TypeRegistrationPlugin,
};
}

Expand Down
30 changes: 29 additions & 1 deletion crates/bevy_core/src/name.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use bevy_ecs::{component::Component, reflect::ReflectComponent};
use bevy_ecs::{
component::Component,
entity::Entity,
prelude::Query,
query::{ReadOnlyWorldQuery, WorldQuery},
reflect::ReflectComponent,
};
use bevy_reflect::Reflect;
use bevy_reflect::{std_traits::ReflectDefault, FromReflect};
use bevy_utils::AHasher;
Expand Down Expand Up @@ -76,6 +82,28 @@ impl std::fmt::Display for Name {
}
}

/// An extension trait for [`Query`] for more user friendly debug information about an entity.
pub trait DebugNameQueryExt<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> {
/// Give the [`Name`] of an entity if it has one, otherwise return the [`Entity`].
fn debug_name(&'w self, entity: Entity) -> Box<dyn std::fmt::Debug + 'w>
where
Q::ReadOnly: WorldQuery<Item<'w> = &'w Name>;
}

impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> DebugNameQueryExt<'w, 's, Q, F>
for Query<'w, 's, Q, F>
{
fn debug_name(&'w self, entity: Entity) -> Box<dyn std::fmt::Debug + 'w>
where
Q::ReadOnly: WorldQuery<Item<'w> = &'w Name>,
{
match self.get(entity).ok() {
Some(name) => Box::new(name.as_str()),
None => Box::new(entity),
}
}
}

/* Conversions from strings */

impl From<&str> for Name {
Expand Down