examples/observers/
observer_basics.rs1use 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
11fn main() {
12 let world = World::new();
13
14 world
15 .observer::<flecs::OnAdd, ()>()
16 .with(Position::id())
17 .each_iter(|it, index, _| {
18 println!(" - OnAdd: {}: {}", it.event_id().to_str(), it.entity(index));
22 });
23
24 world
26 .observer::<flecs::OnSet, &Position>()
27 .add_event(flecs::OnRemove)
28 .each_iter(|it, index, pos| {
29 println!(
30 " - {}: {}: {}: with {:?}",
31 it.event().name(),
32 it.event_id().to_str(),
33 it.entity(index),
34 pos
35 );
36 });
37
38 let entity = world.entity_named("e1").set(Position { x: 10.0, y: 20.0 });
40
41 entity.remove(Position::id());
43
44 entity.remove(Position::id());
46
47 }
52
53#[cfg(feature = "flecs_nightly_tests")]
54#[test]
55fn test() {
56 let output_capture = OutputCapture::capture().unwrap();
57 main();
58 output_capture.test("observer_basics".to_string());
59}