-
Is there a way to print a list of all components on an entity for debugging purposes? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
Here's a helper function for collecting the pub fn get_components_for_entity<'a>(
entity: &Entity,
archetypes: &'a Archetypes,
) -> Option<impl Iterator<Item = ComponentId> + 'a> {
for archetype in archetypes.iter() {
if archetype.entities().contains(entity) {
return Some(archetype.components());
}
}
None
} And you can use it like this: // The system to print the components
pub fn my_system(archetypes: &Archetypes, components: &Components, /** ... */) {
// ...
for comp_id in get_components_for_entity(&my_entity, archetypes).unwrap() {
if let Some(comp_info) = components.get_info(comp_id) {
println!("Component: {:?}", comp_info);
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Here's a modification that I use: pub fn all_component_ids<'a>(
world: &'a World,
entity: Entity,
) -> impl Iterator<Item = ComponentId> + 'a {
let components = world.components();
for archetype in world.archetypes().iter() {
if archetype.entities().iter().any(|e| e.entity() == entity) {
return archetype.components();
}
}
world.archetypes().empty().components()
}
pub fn all_component_infos<'a>(
world: &'a World,
entity: Entity,
) -> impl Iterator<Item = &'a ComponentInfo> + 'a {
let components = world.components();
all_component_ids(world, entity).map(|id| {
components
.get_info(id)
.expect("Component id without info, this shouldnt happen..")
})
} Usage: let names = all_component_infos(&app.world, entity)
.map(|info| info.name())
.collect::<Vec<_>>()
.join("\n\n");
println!("{names}"); |
Beta Was this translation helpful? Give feedback.
-
Update: This can be done a lot simpler now with just a fn my_system(world: &World) {
// ...
println!("{:#?}", world.inspect_entity(my_entity));
}
|
Beta Was this translation helpful? Give feedback.
Update: This can be done a lot simpler now with just a
World
reference usingWorld::inspect_entity
.