Skip to content

Improving docs for Named Entity References (the #MyName inside bsn) - #26179

Open
erickpiva wants to merge 6 commits into
bevyengine:mainfrom
erickpiva:scene_docs
Open

erickpiva wants to merge 6 commits into
bevyengine:mainfrom
erickpiva:scene_docs

Conversation

@erickpiva

Copy link
Copy Markdown

Objective

Improve clarity around Named Entity References, how to pass the EntityTemplate around and storing the entity in a Component

Solution

  • Expand the docs about Named Entity Reference in bevy_scene docs, making the text clearer.
  • Add examples about the EntityTemplate generated, how to pass it to functions and how to store the entity generated in a component
  • Include a few of these concepts in the scene/bsn.rs example.

Testing

  • Generate the docs locally and visually checked the result.
  • Compile and run the bsn examples with no error and no warnings.

Duplicate of #26146 because I messed up :)

erickpiva and others added 4 commits October 14, 2026 16:13
Updates the bsn example to show how a EntityTemplate can be passed
around functions and store in a component for later use
Clearing up the text in the "Named Entity Reference" section in
bevy_scene docs and adding a few more examples on how this can be used
Co-authored-by: Martín Maita <47983254+mnmaita@users.noreply.github.com>
//! ```ignore
//! bsn! {
//! #Root
//! Reference(#Child)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you say more about why this was swapped? I think that showing the basic form first is a bit clearer pedagogically.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The basic form is showed in the example above (lines 161). The text above this example comments about that, and says that Ancestors can access their descendants but it was never showed.

Comment thread crates/bevy_scene/src/lib.rs Outdated
//! Using `#Name` as a value in [`bsn!`] will result in an [`EntityTemplate`], which is a [`Template`] that resolves to an [`Entity`]
//! [`Component`]s with [`Entity`] fields should generally derive [`FromTemplate`], because [`Entity`] uses [`FromTemplate`] to map to [`EntityTemplate`].
//!
//! ```rust, ignore

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should avoid ignoring doc tests when possible to improve robustness when refactoring. It looks like that's the style here, but you can make them compile using # to hide the import lines.

Not blocking though.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I'll keep it in mind for future doc work. I blindly followed the style in this page. The igonore is removed from my code. I tried to remove from the rest of the main page, but rustdoc --test bevy_scene/src/lib.rs included erroneous code from all the crate documentation and I got overwhelmed lol.

Comment thread examples/scene/bsn.rs Outdated
BackgroundColor(Color::srgb(0.15, 0.15, 0.15))
on(|event: On<PointerPress>, q_reference: Query<&Reference> |{
let ui_holder = q_reference.get(event.entity).expect("We are sure it exists");
println!("I have a reference to this entity => {:#?}", ui_holder.0);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This language is a bit confusing: it's not a reference in the Rust sense.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

My idea was to keep the language of the docs, were I use Reference as the component to hold the Entity, and this just came together. I agree it could be somewhat confusing. I'll refactor it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I changed to RootUi, it should be more descriptive and shows a potential use case =)

@alice-i-cecile alice-i-cecile added C-Docs An addition or correction to our documentation A-Scenes Composing and serializing ECS objects S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged X-Uncontroversial This work is generally agreed upon D-Straightforward Simple bug fixes and API improvements, docs, test and examples labels Oct 14, 2026

@zincdev0 zincdev0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good, I left some (albeit pedantic) notes in the example.

Comment thread examples/scene/bsn.rs

fn ui() -> impl Scene {
bsn! {
#MainUI

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The naming difference between #MainUI and struct RootUi, which represent the same thing, could be confusing.

Comment thread examples/scene/bsn.rs
fn button(label: &str, root_entity: EntityTemplate) -> impl Scene {
bsn! {
Button
RootUi( root_entity )

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The whitespace around root_entity would be removed by rustfmt, if this wasn't a bsn! block.

This branch has not been deployed

No deployments
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-Docs An addition or correction to our documentation D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged X-Uncontroversial This work is generally agreed upon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants