examples/systems/
system_time_interval.rs

1use crate::z_ignore_test_common::*;
2
3use flecs_ecs::prelude::*;
4// This example shows how to run a system at a specified time interval.
5
6#[derive(Component)]
7struct Timeout {
8    pub value: f32,
9}
10
11fn tick(mut it: TableIter) {
12    while it.next() {
13        println!("{}", it.system().name());
14    }
15}
16
17fn main() {
18    let world = World::new();
19
20    world.set(Timeout { value: 3.5 });
21
22    world
23        .system::<&mut Timeout>()
24        .each_iter(|it, _index, timeout| {
25            timeout.value -= it.delta_time();
26        });
27
28    world.system_named::<()>("Tick").interval(1.0).run(tick);
29
30    world.system_named::<()>("FastTick").interval(0.5).run(tick);
31
32    // Run the main loop at 60 FPS
33    world.set_target_fps(60.0);
34
35    while world.progress() {
36        if world.map::<&Timeout, _>(|timeout| timeout.value <= 0.0) {
37            println!("Timed out!");
38            break;
39        }
40    }
41
42    // Output:
43    // FastTick
44    // Tick
45    // FastTick
46    // FastTick
47    // Tick
48    // FastTick
49    // FastTick
50    // Tick
51    // FastTick
52    // FastTick
53    // Timed out!
54}
55
56#[cfg(feature = "flecs_nightly_tests")]
57#[test]
58fn test() {
59    let output_capture = OutputCapture::capture().unwrap();
60    main();
61    assert!(output_capture.output_string().contains("Timed out!"));
62}