Skip to content

Commit 8f76330

Browse files
committed
Add dynamic schedule example
1 parent 8d4f86b commit 8f76330

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,10 @@ path = "examples/ecs/timers.rs"
317317
name = "query_bundle"
318318
path = "examples/ecs/query_bundle.rs"
319319

320+
[[example]]
321+
name = "dynamic_schedule"
322+
path = "examples/ecs/dynamic_schedule.rs"
323+
320324
# Games
321325
[[example]]
322326
name = "alien_cake_addict"

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ Example | File | Description
170170
`system_param` | [`ecs/system_param.rs`](./ecs/system_param.rs) | Illustrates creating custom system parameters with `SystemParam`
171171
`system_sets` | [`ecs/system_sets.rs`](./ecs/system_sets.rs) | Shows `SystemSet` use along with run criterion
172172
`timers` | [`ecs/timers.rs`](./ecs/timers.rs) | Illustrates ticking `Timer` resources inside systems and handling their state
173+
`dynamic_schedule` | [`ecs/dynamic_schedule.rs`](./ecs/dynamic_schedule.rs) | Shows how to modify the schedule from systems
173174

174175
## Games
175176

examples/ecs/dynamic_schedule.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use bevy::{core::FixedTimestep, prelude::*};
2+
use rand::Rng;
3+
4+
fn main() {
5+
App::build()
6+
.add_plugins(DefaultPlugins)
7+
.add_stage_after(
8+
CoreStage::Update,
9+
"slow",
10+
SystemStage::parallel().with_run_criteria(FixedTimestep::step(1.0)),
11+
)
12+
.add_startup_system(setup)
13+
.add_system(dynamic.with_run_criteria(FixedTimestep::step(1.0)))
14+
.run();
15+
}
16+
17+
fn setup(mut commands: Commands) {
18+
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
19+
}
20+
21+
fn dynamic(mut commands: Commands, mut system_counter: Local<u64>) {
22+
let count = system_counter.clone();
23+
*system_counter += 1;
24+
let closure = move |mut commands: Commands,
25+
asset_server: Res<AssetServer>,
26+
mut materials: ResMut<Assets<ColorMaterial>>| {
27+
info!("Hello from system {}", count);
28+
29+
let mut rng = rand::thread_rng();
30+
let texture_handle = asset_server.load("branding/icon.png");
31+
commands.spawn_bundle(SpriteBundle {
32+
material: materials.add(texture_handle.into()),
33+
transform: Transform::from_xyz(rng.gen_range(-400f32..400f32), rng.gen_range(-400f32..400f32), 0.0),
34+
..Default::default()
35+
});
36+
};
37+
commands.insert_system(closure, "slow");
38+
}

0 commit comments

Comments
 (0)