[英]How can I read from a component and write to a new entity with that same component in Rust SPECS?
我有一個正在生成其他實體的實體。 例如,生成器有一個位置組件,我希望生成的實體與生成器具有相同的位置。
在生成系統中,好像需要讀寫一個組件,這聽起來不太可能。 唯一的選擇似乎是LazyUpdate
,但我想避免這種情況,因為它需要調用world::maintain
,並且我想在同一框架內的另一個系統中使用生成的實體。
我的系統代碼:
#[derive(Debug)]
struct Position {
x: f32, // meters
y: f32,
z: f32,
direction: f32,
}
impl Component for Position {
type Storage = VecStorage<Self>;
}
struct GenerateEntity;
impl<'a> System<'a> for GenerateEntity {
type SystemData = (
ReadStorage<'a, Generator>,
ReadStorage<'a, Position>,
Entities<'a>,
);
fn run(&mut self, (gen, pos, entities): Self::SystemData) {
for (pos, gen) in (&pos, &gen).join() {
let generated = entities.create();
// This gives an error because position can only be read
pos.insert(generated, pos.clone());
}
}
}
我該如何解決這個問題?
似乎我需要同時讀取和編寫一個組件,這聽起來不可能
當然是:使用WriteStorage
。
這個名字有點誤導。 WriteStorage
不是只寫存儲; 它是可變存儲,包括閱讀。
唯一的問題是您可能無法在迭代時插入位置存儲。 您需要存儲您想要在循環期間進行的更改並在之后應用它們。
(正如評論指出的那樣,您應該重命名循環中的pos
(指的是單個組件),以便它不會影響您作為參數的pos
(指的是整個存儲))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.