SIGN IN SIGN UP
bevyengine / bevy UNCLAIMED

A refreshingly simple data-driven game engine built in Rust

0 0 59 Rust

bsn: Allow passing EntityTemplate into scene functions and Scene Component @props (#24174)

# Objective
Prior PR, see for somewhat outdated description regarding global entity
indices:
- #24173

Currently, its not possible to pass an entity name reference to a scene
function (`-> impl Scene`) or a Scene Component `@props`

This PR enables this, based on #24173 by allowing easy passing on
EntityTemplate:
```rs
 #[derive(Component, FromTemplate)]
struct Reference(Entity);
fn widget(entity: EntityTemplate) -> impl Scene {
    bsn! {
        Reference(entity)
    }
}
bsn! {
    #Name
    Children [
        :widget(#Name)
    ]
};
```

## Solution

I tried this before in
https://github.com/bevyengine/bevy/compare/main...laundmo:bevy:bsn-hacky-name-passing
but had to compromise so it was obvious it wasn't going to ever be
merged. After talking with @cart on discord about this [around
here](https://discord.com/channels/691052431525675048/1264881140007702558/1501663053551239209)
i went with attempting the global entitiy indices, which resulted in
#24173 (all of those changes also being part of this PR)

## Testing

- [x] `cargo test -p bevy_scene --lib` with new tests for this feature
- [x] `cargo run --example feathers_gallery --features=bevy_feathers`
still works the same
- [x] new basic benchmark for name resolution doesn't show any major
regression

---

## Showcase

In a `bsn!` macro, its now possible to pass a `#Name` entity reference
into another Scene as an `EntityTemplate`.

To use this, write a scene function (or scene component prop) which
takes `EntityTemplate`. Components which contain an `Entity` and
implement `FromTemplate` can directly take in `EntityTemplate`

```rust
#[derive(Component, FromTemplate)]
struct FooBar(Entity);
        
fn foo(entity: EntityTemplate) -> impl Scene {
    bsn! {
        Reference(entity)
    }
}
```
Now, theres multiple ways to pass in an `EntityTemplate` like this,
heres some examples:
```rust
bsn! {
	#SomeName
    Children [
        :foo(#SomeName),
		foo(#SomeName)
		foo(#{some_entity_value}) // some_entity_value: any expression evaluating to Entity, most likely a variable
		:Bar {
		  @myprop: #SomeName
		}
    ]
}
```

---------

Co-authored-by: Carter Anderson <mcanders1@gmail.com>
L
laund committed
81520a73c8dc98cf398768878b9e874e47b26227
Parent: 370be1b
Committed by GitHub <noreply@github.com> on 5/17/2026, 1:24:59 PM