查询集
为了安全起见,一个系统不能在同一个组件上有多个具有可变性冲突的查询。
Bevy 提供了一个解决方案:将它们包裹在一个 QuerySet
中:
fn reset_health(
// access the health of enemies and the health of players
// (note: some entities could be both!)
mut q: QuerySet<(
QueryState<&mut Health, With<Enemy>>,
QueryState<&mut Health, With<Player>>
)>,
) {
// set health of enemies
for mut health in q.q0().iter_mut() {
health.hp = 50.0;
}
// set health of players
for mut health in q.q1().iter_mut() {
health.hp = 100.0;
}
}
(注意:你必须使用 QueryState
而不是 Query
)
这可以确保在同一时间只能使用其中一个有冲突的查询。
一个查询集的最大查询数是 4。