Bevy Version: | 0.9 | (outdated!) |
---|
Param Sets
For safety reasons, a system cannot have multiple parameters whose data access might have a chance of mutability conflicts over the same data.
Some examples:
- Multiple incompatible queries.
- Using
&World
while also having other system parameters to access specific data. - …
Bevy provides a solution: wrap them in a ParamSet
:
fn reset_health(
// access the health of enemies and the health of players
// (note: some entities could be both!)
mut set: ParamSet<(
Query<&mut Health, With<Enemy>>,
Query<&mut Health, With<Player>>,
// also access the whole world ... why not
&World,
)>,
) {
// set health of enemies (use the 1st param in the set)
for mut health in set.p0().iter_mut() {
health.hp = 50.0;
}
// set health of players (use the 2nd param in the set))
for mut health in set.p1().iter_mut() {
health.hp = 100.0;
}
// read some data from the world (use the 3rd param in the set)
let my_resource = set.p2().resource::<MyResource>();
// since we only used the conflicting system params one at a time,
// everything is safe and our code can compile; ParamSet guarantees this
}
This ensures only one of the conflicting parameters can be used at the same time.
The maximum number of parameters in a param set is 8.