Skip to content

Commit

Permalink
Use try_insert to prevent panics
Browse files Browse the repository at this point in the history
When using `insert` instead of `try_insert` on `EntityCommand`, it can
happen that the system panics, when the entity does not exist, even
when the `EntityCommand` has been accessed with `get_entity(...)`.
The following example demonstrates the problem:

```rust
if let Some(mut entity_commands) = commands.get_entity(entity) {
    // at this point some other system despawns `entity` concurrently
    entity_commands.insert(bundle);
    //              ^^^^^^ this will panic
}
```

This is also documented in the official bevy docs:
https://docs.rs/bevy/0.12.1/bevy/ecs/system/struct.Commands.html#method.get_entity
  • Loading branch information
Jan Riemer committed Jan 2, 2024
1 parent 1471a9f commit d70def5
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion crates/bevy_picking_core/src/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ pub fn update_interactions(
if let Ok(mut interaction) = interact.get_mut(hovered_entity) {
*interaction = new_interaction;
} else if let Some(mut entity_commands) = commands.get_entity(hovered_entity) {
entity_commands.insert(new_interaction);
entity_commands.try_insert(new_interaction);
}
}
}
Expand Down

0 comments on commit d70def5

Please sign in to comment.