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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use crate::z_ignore_test_common::*;
use flecs_ecs::prelude::*;
#[derive(Component)]
struct Timeout {
pub value: f32,
}
fn main() {
let world = World::new();
// System that deletes an entity after a timeout expires
world
.system::<&mut Timeout>()
.each_iter(|it, index, timeout| {
timeout.value -= it.delta_time();
if timeout.value <= 0.0 {
// Delete the entity
// To make sure that the storage doesn't change while a system
// is iterating entities, and multiple threads can safely access
// the data, mutations (like a delete) are added to a command
// queue and executed when it's safe to do so.
// When the entity to be mutated is not the same as the entity
// provided by the system, an additional mut() call is required.
// See the mutate_entity_handle example.
let e = it.entity(index);
e.destruct();
println!("Expire: {} deleted!", e.name());
}
});
// System that prints remaining expiry time
world.system::<&Timeout>().each_entity(|e, timeout| {
println!(
"PrintExpire: {} has {:.2} seconds left",
e.name(),
timeout.value
);
});
// Observer that triggers when entity is actually deleted
world
.observer::<flecs::OnRemove, &Timeout>()
.each_entity(|e, _timeout| {
println!("Expired: {} actually deleted", e.name());
});
let e = world.entity_named("MyEntity").set(Timeout { value: 2.5 });
world.set_target_fps(1.0);
while world.progress() {
// If entity is no longer alive, exit
if !e.is_alive() {
break;
}
println!("Tick...");
}
// Output:
// PrintExpire: MyEntity has 2.00 seconds left
// Tick...
// PrintExpire: MyEntity has 0.99 seconds left
// Tick...
// Expire: MyEntity deleted!
// PrintExpire: MyEntity has -0.03 seconds left
// Expired: MyEntity actually deleted
}
#[cfg(feature = "flecs_nightly_tests")]
#[test]
fn test() {
let output_capture = OutputCapture::capture().unwrap();
main();
assert!(output_capture.output_string().contains("deleted"));
}