Flush commands between lifecycle events while processing observers
- Dominant language
- Rust
- Stars
- 48.2k
- Forks
- 4.8k
- Avg merge
- 3d 22h
- Merged PRs (30d)
- 161
Description
## Bevy version and features
0.16
## What you did
I have a component that represents a togglable button:
```rust
#[derive(Component, Deref, Clone, Copy)]
#[component(immutable)]
#[require(
Replicated,
Button,
Node {
width: Val::Px(150.0),
height: Val::Px(65.0),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..Default::default()
},
)]
struct TogglableButton(bool);
```
Then I try to react on its insertion this way:
/// Spawn children.
```rust
fn init_button(trigger: Trigger, mut commands: Commands) {
commands.entity(trigger.target()).with_child((
Text::default(),
TextShadow::default(),
TextFont {
font_size: 30.0,
..Default::default()
},
));
}
/// Update text every time the component is inserted.
fn update_button_text(
trigger: Trigger,
children: Query<(&TogglableButton, &Children)>,
mut texts: Query<&mut Text>,
) {
let (&button, children) = children.get(trigger.target()).unwrap();
if let Some(mut text) = texts.iter_many_mut(children).fetch_next() {
text.clear();
if *button {
text.push_str("On");
} else {
text.push_str("Off");
}
}
}
```
## What went wrong
It panics because when `OnInsert` runs children aren't inserted yet.
I'd expect commands to be applied after `OnAdd`.
Contributor guide
Assessment
This issue has not been assessed yet.