Skip to content

Entity path methods and bsn entity path resolving#24018

Merged
alice-i-cecile merged 24 commits intobevyengine:mainfrom
Freyja-moth:bsn_path_resolution
Apr 29, 2026
Merged

Entity path methods and bsn entity path resolving#24018
alice-i-cecile merged 24 commits intobevyengine:mainfrom
Freyja-moth:bsn_path_resolution

Conversation

@Freyja-moth
Copy link
Copy Markdown
Contributor

@Freyja-moth Freyja-moth commented Apr 28, 2026

Objective

Fixes: #16201

Originally based on and closes: #20848

Solution

Add two to methods to world

fn get_entity_from_path<R: Relationship, C: Component + PartialEq + Clone>(
    &self,
    root: Option<Entity>,
    path: &[C],
) -> Option<Entity>;
fn get_path_from_entity<R: Relationship, C: Component + PartialEq + Clone>(
    &self,
    root: Option<Entity>,
    entity: Entity,
) -> Option<Vec<C>>;

Original methods were taken from #20848, but changed to focus on bottom up search instead of a top down search, which should hopefully be more efficient and made them generic over the relationship.

Also added an EntityPath variant to EntityTemplate.

Testing

Tests included and working.

Showcase

#[derive(Component)]
#[relationship(relationship_target = WieldedBy)]
pub struct Weapon(pub Entity);

#[derive(Component)]
#[relationship_target(relationship = Weapon)]
pub struct WieldedBy(Entity):

fn player() -> impl Scene {
    bsn! {
        Player
        Weapon("Items/Weapons/Sword")
    }
}

@Freyja-moth
Copy link
Copy Markdown
Contributor Author

Small note, should I change the methods to have more descriptive errors?

Comment thread crates/bevy_ecs/src/template.rs Outdated
Comment thread crates/bevy_ecs/src/template.rs Outdated
Comment thread crates/bevy_ecs/src/template.rs Outdated
Co-authored-by: Freyja-moth <156322843+Freyja-moth@users.noreply.github.com>
Comment thread crates/bevy_ecs/src/world/error.rs Outdated
@Freyja-moth
Copy link
Copy Markdown
Contributor Author

Currently using an immutable borrow of world.

This does cause it to return None if the relationship component or compared component aren't registered, but that should be fine since if either weren't registered they don't exist in the world and therefore the path was invalid anyways.

@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible A-Scenes Composing and serializing ECS objects D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Apr 28, 2026
@alice-i-cecile alice-i-cecile requested a review from cart April 28, 2026 16:47
Comment thread crates/bevy_ecs/src/world/error.rs Outdated
Comment thread crates/bevy_ecs/src/world/mod.rs Outdated
Comment thread crates/bevy_ecs/src/world/mod.rs Outdated
Comment thread crates/bevy_ecs/src/world/mod.rs Outdated
Copy link
Copy Markdown
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I quite like this idea in practice: this seems extremely useful for both animation and UI. However, I think there are two important cleanups to do:

  1. Make the error handling better.
  2. Extract out a pair of common case methods, and give those the nice names.

@alice-i-cecile alice-i-cecile added the M-Release-Note Work that should be called out in the blog due to impact label Apr 28, 2026
@github-actions
Copy link
Copy Markdown
Contributor

It looks like your PR has been selected for a highlight in the next release blog post, but you didn't provide a release note.

Please review the instructions for writing release notes, then expand or revise the content in the release notes directory to showcase your changes.

@alice-i-cecile alice-i-cecile added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Apr 28, 2026
@Freyja-moth
Copy link
Copy Markdown
Contributor Author

Moved the previous methods to get_entity_from_relationship_path and get_relationship_path_from_entity, then made the methods specialize on ChildOf.

Not fully happy with the names so welcoming bikeshedding.

@Freyja-moth
Copy link
Copy Markdown
Contributor Author

Will fix whatever issues come up tomorrow, I've spent way too much time on this tonight.

Comment thread _release-content/release-notes/entity_path_resolving.md Outdated
Comment thread _release-content/release-notes/entity_path_resolving.md Outdated
Copy link
Copy Markdown
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy with this now, but it still needs a test for the BSN usage.

The release note is super exciting: what an incredibly useful thing to do on cold paths. I've left a few suggestions to improve it.

Good luck with CI 🫡

@Freyja-moth
Copy link
Copy Markdown
Contributor Author

With any luck this should be the last of the changes.

@Freyja-moth
Copy link
Copy Markdown
Contributor Author

Ci passed, I'm free!

Comment thread _release-content/release-notes/entity_path_resolving.md Outdated
@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Apr 29, 2026
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Apr 29, 2026
@beicause
Copy link
Copy Markdown
Member

The implementation differs from the PR description; the path is &str instead of &[C]. What's the reason?

I feel the performance may not be fast, as it involves String and Vec allocations and string comparisons in the loop.

@Freyja-moth
Copy link
Copy Markdown
Contributor Author

Freyja-moth commented Apr 29, 2026

The implementation was changed in response to feedback and I forgot to change the pr description.

As for performance implications, the loop is only ran on all entities that match the top element of the path which shouldn't be too many, and since most usecases will be for initializing scenes, which only happens once and usually with other elements it's probably fine.

If you can find a more optimal way of searching feel free to open a pr after this one.

@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Apr 29, 2026
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Apr 29, 2026
Merged via the queue into bevyengine:main with commit 98c6910 Apr 29, 2026
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Scenes Composing and serializing ECS objects C-Feature A new feature, making something new possible D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes M-Release-Note Work that should be called out in the blog due to impact S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Get Entity from scene hierarchy

4 participants