Does it make sense for components to have a "destructor"?
- Linguagem predominante
- Rust
- Estrelas
- 1.7k
- Forks
- 140
- Métricas de merge de PRs
- Nenhum PR com merge em 30d
Descrição
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?
Guia de contribuição
Nenhum guia de contribuição indexado para este repositório
Avaliação
Esta issue ainda não foi avaliada.