forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_param.rs
47 lines (38 loc) · 1.16 KB
/
system_param.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//! This example creates a custom [`SystemParam`] struct that counts the number of players.
use bevy::{ecs::system::SystemParam, prelude::*};
fn main() {
App::new()
.insert_resource(PlayerCount(0))
.add_startup_system(spawn)
.add_system(count_players)
.run();
}
#[derive(Component)]
pub struct Player;
#[derive(Resource)]
pub struct PlayerCount(usize);
/// The [`SystemParam`] struct can contain any types that can also be included in a
/// system function signature.
///
/// In this example, it includes a query and a mutable resource.
#[derive(SystemParam)]
struct PlayerCounter<'w, 's> {
players: Query<'w, 's, &'static Player>,
count: ResMut<'w, PlayerCount>,
}
impl<'w, 's> PlayerCounter<'w, 's> {
fn count(&mut self) {
self.count.0 = self.players.iter().len();
}
}
/// Spawn some players to count
fn spawn(mut commands: Commands) {
commands.spawn(Player);
commands.spawn(Player);
commands.spawn(Player);
}
/// The [`SystemParam`] can be used directly in a system argument.
fn count_players(mut counter: PlayerCounter) {
counter.count();
println!("{} players in the game", counter.count.0);
}