forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add docs & example for SystemParam (bevyengine#1435)
It took me a little while to figure out how to use the `SystemParam` derive macro to easily create my own params. So I figured I'd add some docs and an example with what I learned. - Fixed a bug in the `SystemParam` derive macro where it didn't detect the correct crate name when used in an example (no longer relevant, replaced by bevyengine#1426 - see further) - Added some doc comments and a short example code block in the docs for the `SystemParam` trait - Added a more complete example with explanatory comments in examples
- Loading branch information
Showing
5 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use bevy::{ecs::SystemParam, prelude::*}; | ||
|
||
/// This example creates a SystemParam struct that counts the number of players | ||
fn main() { | ||
App::build() | ||
.insert_resource(PlayerCount(0)) | ||
.add_startup_system(spawn.system()) | ||
.add_system(count_players.system()) | ||
.run(); | ||
} | ||
|
||
struct Player; | ||
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)] | ||
pub struct PlayerCounter<'a> { | ||
players: Query<'a, &'a Player>, | ||
count: ResMut<'a, PlayerCount>, | ||
} | ||
|
||
impl<'a> PlayerCounter<'a> { | ||
fn count(&mut self) { | ||
self.count.0 = self.players.iter().len(); | ||
} | ||
} | ||
|
||
/// Spawn some players to count | ||
fn spawn(commands: &mut 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); | ||
} |