| 1 |
--- |
| 2 |
title: "Ready Event" |
| 3 |
authors: ["@cart"] |
| 4 |
pull_requests: [] |
| 5 |
--- |
| 6 |
|
| 7 |
We landed BSN, Bevy's next generation scene system, [in our last release](/news/bevy-0-19). It was missing a key piece though: the ability to easily run logic when a scene is fully "ready" and spawned (ex: all dependencies have loaded, the full hierarchy is present, and all of the initial components are inserted in the scene). This is a critical piece for building cohesive, standalone, composable scenes. It is also necessary to properly layer Bevy logic on top of _other_ scene representations (like glTF). |
| 8 |
|
| 9 |
The closest we had was the `Add` event for a given component, which runs "top down" (meaning children are not available). We needed a "bottom up" equivalent to enable building logic that relies on the complete loaded and spawned scene. |
| 10 |
|
| 11 |
The solution is pretty straightforward: trigger a new `Ready` event for each entity in a spawned scene _after_ the full spawn logic has run for that entity (including its descendants). |
| 12 |
|
| 13 |
This enables the following: |
| 14 |
|
| 15 |
```rust |
| 16 |
#[derive(SceneComponent, Default, Clone)] |
| 17 |
struct Widget; |
| 18 |
|
| 19 |
impl Widget { |
| 20 |
fn scene() -> impl Scene { |
| 21 |
bsn! { |
| 22 |
Node { width: px(100), height: px(100) } |
| 23 |
on(|ready: On<Ready>| { |
| 24 |
info!("The full scene, including 'widget.bsn' contents, is available here") |
| 25 |
}) |
| 26 |
Children [ |
| 27 |
Text("hello"), |
| 28 |
:"widget.bsn" |
| 29 |
] |
| 30 |
} |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
world.spawn(bsn!{ @Widget }) |
| 35 |
``` |