Skip to main content

examples/queries/
query_wildcard.rs

1use crate::z_ignore_test_common::*;
2
3use flecs_ecs::prelude::*;
4pub use flecs_ecs::{core::*, macros::Component};
5
6#[derive(Component)]
7pub struct EatsAmount {
8    pub amount: i32,
9}
10
11#[derive(Component)]
12pub struct Apples;
13
14#[derive(Component)]
15pub struct Pears;
16
17fn main() {
18    let world = World::new();
19
20    // Create a query that matches edible components
21    let query = world.new_query::<&(EatsAmount, flecs::Wildcard)>();
22
23    // Create a few entities that match the query
24    world
25        .entity_named("Bob")
26        .set_pair::<EatsAmount, Apples>(EatsAmount { amount: 10 })
27        .set_pair::<EatsAmount, Pears>(EatsAmount { amount: 5 });
28
29    world
30        .entity_named("Alice")
31        .set_pair::<EatsAmount, Apples>(EatsAmount { amount: 4 });
32
33    // Iterate the query with a flecs::iter. This makes it possible to inspect
34    // the pair that we are currently matched with.
35    query.each_iter(|it, index, eats| {
36        let entity = it.entity(index);
37        let pair = it.pair(0);
38        let food = pair.second_id();
39
40        println!("{} eats {} {}", entity, eats.amount, food);
41    });
42
43    // Output:
44    //  Alice eats 4 Apples
45    //  Bob eats 10 Apples
46    //  Bob eats 5 Pears
47}
48
49#[cfg(feature = "flecs_nightly_tests")]
50#[test]
51fn test() {
52    let output_capture = OutputCapture::capture().unwrap();
53    main();
54    output_capture.test("query_wildcard".to_string());
55}