amethyst / amethyst/legion

Does it make sense for components to have a "destructor"?

未關閉
#108 6 則留言 3 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
Rust
星號
1.7k
分支
140
PR 合併指標
30 天內沒有已合併 PR

描述

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?

貢獻指南

這個儲存庫沒有索引到貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。