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

Add inspect command for debugging entities and their components #1467

Closed
alice-i-cecile opened this issue Feb 18, 2021 · 8 comments
Closed
Labels
A-ECS Entities, components, systems, and events C-Feature A new feature, making something new possible C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Trivial Nice and easy! A great choice to get started with Bevy S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it!

Comments

@alice-i-cecile
Copy link
Member

What problem does this solve or what need does it fill?

Querying for specific components of entity is easy, but finding all of its components is necessarily hard, since it would require an exclusive system. This is particularly challenging for beginners who are attempting to understand the ECS or some specialized feature of it.

What solution would you like?

Add a Command that prints / provides information about the specified entity. Component names would be the MVP, but component contents may also be worth including if a suitable debug method exists for them.

What alternative(s) have you considered?

Implementing Debug for a World or a Query could also work well, and might be a nice complementary solution. See #1130 for this.

Serializing entities into a Scene could also work well, but still requires good introspection capabilities and is somewhat unnatural when you only want to quickly examine the results.

Additional context

This functionality would be very useful in the context of an editor: see bevy-inspector-egui for a wonderful prototype of this in a GUI context.

@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible C-Usability A targeted quality-of-life change that makes Bevy easier to use A-ECS Entities, components, systems, and events D-Trivial Nice and easy! A great choice to get started with Bevy labels Feb 18, 2021
@cart
Copy link
Member

cart commented Feb 18, 2021

I'm thinking that instead of adding new Commands, we should just directly expose the relevant World metadata (world.entities(), world.components(), and world.archetypes()) as new SystemParams. This should be safe because the Entities, Components, and Archetypes collections are read-only during normal system execution, so they can be read in parallel.

That way users would have access to 100% of the metadata that Bevy ECS has access to.

@cart
Copy link
Member

cart commented Feb 18, 2021

(this is using the terminology / apis from the Bevy ECS rewrite that I am currently wrapping up)

@alice-i-cecile
Copy link
Member Author

I'm thinking that instead of adding new Commands, we should just directly expose the relevant World metadata (world.entities(), world.components(), and world.archetypes()) as new SystemParams. This should be safe because the Entities, Components, and Archetypes collections are read-only during normal system execution, so they can be read in parallel.

That way users would have access to 100% of the metadata that Bevy ECS has access to.

Cool, this would be super helpful and totally solve the issue at hand. I'd wanted runtime access to archetype data when trying to help with #1446 the other day too.

@ghost
Copy link

ghost commented Apr 13, 2021

This happened in the 0.5 release right?
So can probably be closed?

@alice-i-cecile
Copy link
Member Author

The required data is now exposed nicely, which is fantastic. But we're still lacking nice helper functions, which are particularly useful for quick debugging.

Entities, Components and Archetypes are exposed to the systems, but you still need to somehow extract the relevant information from them.

After a couple minutes of puzzling, I can't figure out which bits of the bevy_ecs API I need to poke at in order to e.g. get the list of all components on the entity, which points at the need for either a clear example in the docs or a helper function of some sort.

@msklywenn
Copy link
Contributor

msklywenn commented May 13, 2021

@alice-i-cecile Something like this?

fn inspect(
    keyboard: Res<Input<KeyCode>>,
    all_entities: Query<Entity>,
    entities: &Entities,
    archetypes: &Archetypes,
    components: &Components,
) {
    if keyboard.just_pressed(KeyCode::F1) {
        for entity in all_entities.iter() {
            println!("Entity: {:?}", entity);
            if let Some(entity_location) = entities.get(entity) {
                if let Some(archetype) = archetypes.get(entity_location.archetype_id) {
                    for component in archetype.components() {
                        if let Some(info) = components.get_info(component) {
                            println!("\tComponent: {}", info.name());
                        }
                    }
                }
            }
        }
    }
}

(do not press F1 when there are four thousand trees on screen, it takes a while...)

@alice-i-cecile
Copy link
Member Author

@msklywenn yes! Bundling the inner section of that up into an entity command would make for a great debugging tool.

@alice-i-cecile
Copy link
Member Author

We may also want to try printing it's debug value if it exists, but I don't think we have access to the relevant trait data... We'd need a blanket impl for component debugging?

@alice-i-cecile alice-i-cecile added the S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it! label Dec 12, 2021
@bors bors bot closed this as completed in b3fa479 Jun 30, 2022
inodentry pushed a commit to IyesGames/bevy that referenced this issue Aug 8, 2022
# Objective

- Provide a way to see the components of an entity.
- Fixes bevyengine#1467

## Solution

- Add `World::inspect_entity`. It accepts an `Entity` and returns a vector of `&ComponentInfo` that the entity has.
- Add `EntityCommands::log_components`. It logs the component names of the entity. (info level)

---

## Changelog

### Added
- Ability to inspect components of an entity through `World::inspect_entity` or `EntityCommands::log_components`
james7132 pushed a commit to james7132/bevy that referenced this issue Oct 28, 2022
# Objective

- Provide a way to see the components of an entity.
- Fixes bevyengine#1467

## Solution

- Add `World::inspect_entity`. It accepts an `Entity` and returns a vector of `&ComponentInfo` that the entity has.
- Add `EntityCommands::log_components`. It logs the component names of the entity. (info level)

---

## Changelog

### Added
- Ability to inspect components of an entity through `World::inspect_entity` or `EntityCommands::log_components`
ItsDoot pushed a commit to ItsDoot/bevy that referenced this issue Feb 1, 2023
# Objective

- Provide a way to see the components of an entity.
- Fixes bevyengine#1467

## Solution

- Add `World::inspect_entity`. It accepts an `Entity` and returns a vector of `&ComponentInfo` that the entity has.
- Add `EntityCommands::log_components`. It logs the component names of the entity. (info level)

---

## Changelog

### Added
- Ability to inspect components of an entity through `World::inspect_entity` or `EntityCommands::log_components`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Feature A new feature, making something new possible C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Trivial Nice and easy! A great choice to get started with Bevy S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants