examples/systems/
system_sync_point_delete.rs

1use crate::z_ignore_test_common::*;
2
3use flecs_ecs::prelude::*;
4
5#[derive(Debug, Component)]
6pub struct Position {
7    pub x: f32,
8    pub y: f32,
9}
10
11#[derive(Debug, Component)]
12pub struct Velocity {
13    pub x: f32,
14    pub y: f32,
15}
16
17fn main() {
18    let world = World::new();
19
20    // This example shows how to annotate systems that delete entities, in a way
21    // that allows the scheduler to correctly insert sync points. See the
22    // sync_point example for more details on sync points.
23    //
24    // While annotating a system for a delete operation follows the same
25    // design as other operations, one key difference is that a system often
26    // does not know which components a to be deleted entity has. This makes it
27    // impossible to annotate the system in advance for specific components.
28    //
29    // To ensure the scheduler is still able to insert the correct sync points,
30    // a system can use a wildcard to indicate that any component could be
31    // modified by the system, which forces the scheduler to insert a sync.
32
33    // Basic move system.
34    world
35        .system_named::<(&mut Position, &Velocity)>("Move")
36        .each(|(p, v)| {
37            p.x += v.x;
38            p.y += v.y;
39        });
40
41    // Delete entities when p.x >= 3. Add wildcard annotation to indicate any
42    // component could be written by the system. Position itself is added as
43    // const, since inside the system we're only reading it.
44    world
45        .system_named::<&Position>("DeleteEntity")
46        .write::<flecs::Wildcard>()
47        .each_entity(|e, p| {
48            if p.x >= 3.0 {
49                println!("Delete entity {}", e.name());
50                e.destruct();
51            }
52        });
53
54    // Print resulting Position. Note that this system will never print entities
55    // that have been deleted by the previous system.
56    world
57        .system_named::<&Position>("PrintPosition")
58        .each_entity(|e, p| {
59            println!("{}: {{ {}, {} }}", e.name(), p.x, p.y);
60        });
61
62    // Create a few test entities for a Position, Velocity query
63    world
64        .entity_named("e1")
65        .set(Position { x: 0.0, y: 0.0 })
66        .set(Velocity { x: 1.0, y: 2.0 });
67
68    world
69        .entity_named("e2")
70        .set(Position { x: 1.0, y: 2.0 })
71        .set(Velocity { x: 1.0, y: 2.0 });
72
73    // Run systems. Debug logging enables us to see the generated schedule.
74    // NOTE flecs C / flecs_ecs_sys needs to be build in debug mode to see the logging.
75    // use the feature flag "sys_build_debug" to enable debug build of flecs C.
76
77    set_log_level(1);
78
79    while world.progress() {
80        if world.count::<Position>() == 0 {
81            break; // No more entities left with Position
82        }
83    }
84    set_log_level(-1);
85
86    // world
87    //     .get::<Snap>()
88    //     .test("system_sync_point_delete".to_string()));
89
90    // Output:
91    //  info: pipeline rebuild
92    //  info: | schedule: threading: 0, staging: 1:
93    //  info: | | system Move
94    //  info: | | system DeleteEntity
95    //  info: | | merge
96    //  info: | schedule: threading: 0, staging: 1:
97    //  info: | | system PrintPosition
98    //  info: | | merge
99    //  e1: { 1, 2 }
100    //  e2: { 2, 4 }
101    //  Delete entity e2
102    //  e1: { 2, 4 }
103    //  Delete entity e1
104
105    // Removing the wildcard annotation from the DeleteEntity system will
106    // remove the first sync point.
107
108    // Note how after both entities are deleted, all three systems will be de-activated and not ran by the scheduler
109}
110
111#[cfg(feature = "flecs_nightly_tests")]
112#[test]
113#[ignore = "`set_log_level` is not safe in parallel tests"]
114fn test() {
115    let output_capture = OutputCapture::capture().unwrap();
116    main();
117    output_capture.test("system_sync_point_delete".to_string());
118}