examples/relationships/relationships_symmetric.rs
1use crate::z_ignore_test_common::*;
2
3use flecs_ecs::prelude::*;
4#[derive(Component)]
5struct TradesWith;
6
7fn main() {
8 let world = World::new();
9
10 // Register TradesWith as symmetric relationship. Symmetric relationships
11 // go both ways, adding (R, B) to A will also add (R, A) to B.
12 world.component::<TradesWith>().add(flecs::Symmetric);
13
14 // Create two players
15 let player_1 = world.entity();
16 let player_2 = world.entity();
17
18 // Add (TradesWith, player_2) to player_1. This also adds
19 // (TradesWith, player_1) to player_2.
20 player_1.add((TradesWith, player_2));
21
22 // Log platoon of unit
23 println!(
24 "Player 1 trades with Player 2: {}",
25 player_1.has((TradesWith, player_2))
26 ); // true
27 println!(
28 "Player 2 trades with Player 1: {}",
29 player_2.has((TradesWith, player_1))
30 ); // true
31
32 // Output:
33 // Player 1 trades with Player 2: true
34 // Player 2 trades with Player 1: true
35}
36
37#[cfg(feature = "flecs_nightly_tests")]
38#[test]
39fn test() {
40 let output_capture = OutputCapture::capture().unwrap();
41 main();
42 output_capture.test("relationships_symmetric".to_string());
43}