-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathcustom_executor.rs
More file actions
56 lines (45 loc) · 1.35 KB
/
custom_executor.rs
File metadata and controls
56 lines (45 loc) · 1.35 KB
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
48
49
50
51
52
53
54
55
56
//! Demonstrates how to make a custom [`SystemExecutor`].
use bevy::{
ecs::{
error::{BevyError, ErrorContext},
schedule::{FixedBitSet, SystemExecutor, SystemSchedule},
},
prelude::*,
};
#[derive(Default)]
struct CustomExecutor;
impl SystemExecutor for CustomExecutor {
fn init(&mut self, _schedule: &SystemSchedule) {}
fn run(
&mut self,
schedule: &mut SystemSchedule,
world: &mut World,
_skip_systems: Option<&FixedBitSet>,
_error_handler: fn(BevyError, ErrorContext),
) {
#[expect(unsafe_code, reason = "CustomExecutor's require unsafe")]
// SAFETY: `run` is a trait method on `System`
for entry in unsafe { schedule.systems_mut().iter_mut() } {
let _ = entry.run((), world);
}
}
fn set_apply_final_deferred(&mut self, _value: bool) {}
}
#[derive(Resource, Default)]
struct Counter(u32);
fn increment(mut counter: ResMut<Counter>) {
counter.0 += 1;
}
fn print_counter(counter: Res<Counter>) {
println!("Counter: {}", counter.0);
}
fn main() {
let mut world = World::new();
world.init_resource::<Counter>();
let mut schedule = Schedule::default();
schedule.set_executor(CustomExecutor);
schedule.add_systems((increment, print_counter).chain());
for _ in 0..5 {
schedule.run(&mut world);
}
}