Skip to main content

examples/systems/
system_custom_phases.rs

1use crate::z_ignore_test_common::*;
2
3use flecs_ecs::prelude::*;
4// This application demonstrates how to use custom phases for systems. The
5// default pipeline will automatically run systems for custom phases as long as
6// they have the flecs::Phase tag.
7
8// Dummy system
9fn sys(mut it: TableIter) {
10    while it.next() {
11        println!("system {}", it.system().name());
12    }
13}
14
15fn main() {
16    let world = World::new();
17
18    // Create two custom phases that branch off of EcsOnUpdate. Note that the
19    // phases have the Phase tag, which is necessary for the builtin pipeline
20    // to discover which systems it should run.
21    let physics = world
22        .entity()
23        .add(flecs::pipeline::Phase)
24        .depends_on(id::<flecs::pipeline::OnUpdate>());
25
26    let collisions = world
27        .entity()
28        .add(id::<flecs::pipeline::Phase>())
29        .depends_on(physics);
30
31    // Create 3 dummy systems.
32    world
33        .system_named::<()>("CollisionSystem")
34        .kind(collisions)
35        .run(sys);
36
37    world
38        .system_named::<()>("PhysicsSystem")
39        .kind(physics)
40        .run(sys);
41
42    world
43        .system_named::<()>("GameSystem")
44        .kind(id::<flecs::pipeline::OnUpdate>())
45        .run(sys);
46
47    // Run pipeline
48    world.progress();
49
50    // Output:
51    //   system GameSystem
52    //   system PhysicsSystem
53    //   system CollisionSystem
54}
55
56#[cfg(feature = "flecs_nightly_tests")]
57#[test]
58fn test() {
59    let output_capture = OutputCapture::capture().unwrap();
60    main();
61    output_capture.test("system_custom_phases".to_string());
62}