Does it make sense for components to have a "destructor"?
- Lenguaje dominante
- Rust
- Estrellas
- 1.7k
- Forks
- 140
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Descripción
It's pretty common for one component to reference another `Entity` with some sort of has-a relationship (e.g. a player has several limbs), and when the parent gets removed from the world we need to also delete the things it references.
Elsewhere you might implement this sort of thing with a `Drop` impl, but to delete associated items we'd need access to the `World`. What about giving `Component` a `before_delete()` method that gets called when an `Entity` is destroyed?
I was thinking something along the lines of this:
```rust
pub trait Component: Send + Sync + 'static {
/// A callback triggered when an entity is deleted, immediately before its
/// components are removed and destroyed.
fn before_delete(&mut self, world: &mut World) {}
}
struct Player {
left_arm: Entity,
right_arm: Entity,
left_leg: Entity,
right_leg: Entity,
}
impl Component for Player {
fn before_delete(&mut self, world: &mut World) {
world.delete(self.left_arm);
...
}
}
```
Does this seem like something that would be used often enough to warrant implementing?
Guía de contribución
No hay ninguna guía de contribución indexada para este repositorio
Evaluación
Este issue todavía no se ha evaluado.