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] - Add ability to inspect entity's components #5136

Closed
wants to merge 7 commits into from
Closed
Changes from 1 commit
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
27 changes: 15 additions & 12 deletions crates/bevy_ecs/src/system/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
entity::{Entities, Entity},
world::{FromWorld, World},
};
use bevy_utils::tracing::{debug, error, warn};
use bevy_utils::tracing::{error, info, warn};
pub use command_queue::CommandQueue;
pub use parallel_scope::*;
use std::marker::PhantomData;
Expand Down Expand Up @@ -217,11 +217,6 @@ impl<'w, 's> Commands<'w, 's> {
}
}

/// Logs the components of a given entity at the debug level.
pub fn debug_entity(&mut self, entity: Entity) {
self.queue.push(DebugEntity { entity });
}

/// Spawns entities to the [`World`] according to the given iterator (or a type that can
/// be converted to it).
///
Expand Down Expand Up @@ -593,6 +588,13 @@ impl<'w, 's, 'a> EntityCommands<'w, 's, 'a> {
});
}

/// Logs the components of the entity at the debug level.
pub fn log_components(&mut self) {
self.commands.add(DebugEntity {
entity: self.entity,
});
}

/// Returns the underlying [`Commands`].
pub fn commands(&mut self) -> &mut Commands<'w, 's> {
self.commands
Expand Down Expand Up @@ -798,18 +800,19 @@ impl<R: Resource> Command for RemoveResource<R> {
}
}

/// [`Command`] to log the components of a given entity. See [`Commands::debug_entity`].
/// [`Command`] to log the components of a given entity. See [`EntityCommands::log_components`].
pub struct DebugEntity {
entity: Entity,
}

impl Command for DebugEntity {
fn write(self, world: &mut World) {
debug!(
"Entity {:?}: {:?}",
self.entity,
world.inspect_entity(self.entity)
);
let debug_infos: Vec<_> = world
.inspect_entity(self.entity)
.into_iter()
.map(|component_info| component_info.name())
.collect();
info!("Entity {:?}: {:?}", self.entity, debug_infos);
}
}

Expand Down