From 031e6aec86717104934b08bbe265e76041939b33 Mon Sep 17 00:00:00 2001 From: Indradb <60851042+Indra-db@users.noreply.github.com> Date: Sat, 23 Mar 2024 21:18:12 +0900 Subject: [PATCH] update pair functions to support 2 pairs with data + update example w/ this change. --- .../examples/relationships_component_data.rs | 50 ++++++++++++++----- flecs_ecs/src/core/entity.rs | 4 +- flecs_ecs/src/core/entity_view.rs | 2 +- flecs_ecs/src/core/world.rs | 2 +- 4 files changed, 42 insertions(+), 16 deletions(-) diff --git a/flecs_ecs/examples/relationships_component_data.rs b/flecs_ecs/examples/relationships_component_data.rs index 813f124b..8b10bba3 100644 --- a/flecs_ecs/examples/relationships_component_data.rs +++ b/flecs_ecs/examples/relationships_component_data.rs @@ -56,15 +56,22 @@ fn main() { // Note that and are two // different pairs, and can be added to an entity at the same time. - // Currently there's no support in Rust flecs for both parts of a pair to be components with data, One must be empty. - // in Flecs C++ it is supported, but one can only be set at a time. + // If both parts of a pair are components, the pair assumes the type of + // the first element: + let e3 = world + .new_entity() + .set_pair_first::(Expires { timeout: 0.5 }); + + let expires = e3.get_pair_first::(); + println!("expires: {}", expires.unwrap().timeout); - /* missing example here since a decision was made to not support this due to the unsafe nature of it - of users possibly transmuting wrong data and crashing their code base. It's open for discussion to add this in. - Currently it's a not common use case. + // You can prevent a pair from assuming the type of a component by adding + // the Tag property to a relationship: + world.component::().add_id(ECS_TAG); - due to this, the tag property isn't supported either. We're missing an essential - rust feature to essentially safely support this feature */ + // Even though Position is a component, contains no + // data because MustHave has the Tag property. + world.new_entity().add_pair::(); // The id::type_id method can be used to find the component type for a pair: println!( @@ -83,6 +90,22 @@ fn main() { .get_hierarchy_path() .unwrap() ); + println!( + "{}", + world + .get_id_pair::() + .type_id() + .get_hierarchy_path() + .unwrap() + ); + println!( + "{}", + world + .get_id_pair::() + .type_id() + .get_hierarchy_path() + .unwrap() + ); // When querying for a relationship component, add the pair type as template // argument to the builder: @@ -97,9 +120,12 @@ fn main() { }); // Output: - // e1: requires: 1.21 - // e1: requires: 1.5 - // ::Requires - // ::Requires - // requires: 1.21 gigawatts + // e1: requires: 1.21 + // e1: requires: 1.5 + // expires: 0.5 + // ::Requires + // ::Requires + // ::Expires + // 0 + // requires: 1.21 gigawatts } diff --git a/flecs_ecs/src/core/entity.rs b/flecs_ecs/src/core/entity.rs index 2b979746..18e671f0 100644 --- a/flecs_ecs/src/core/entity.rs +++ b/flecs_ecs/src/core/entity.rs @@ -1127,7 +1127,7 @@ impl Entity { pub fn set_pair_first(self, first: First) -> Self where First: CachedComponentData + ComponentType + NotEmptyComponent, - Second: CachedComponentData + ComponentType + EmptyComponent, + Second: CachedComponentData + ComponentType, { set_helper( self.world, @@ -1928,7 +1928,7 @@ impl Entity { pub fn get_pair_first_mut(&mut self) -> &mut First where First: CachedComponentData + ComponentType + NotEmptyComponent, - Second: CachedComponentData + ComponentType + EmptyComponent, + Second: CachedComponentData + ComponentType, { self.get_pair_first_id_mut(Second::get_id(self.world)) } diff --git a/flecs_ecs/src/core/entity_view.rs b/flecs_ecs/src/core/entity_view.rs index df7a27fb..4b0ccf15 100644 --- a/flecs_ecs/src/core/entity_view.rs +++ b/flecs_ecs/src/core/entity_view.rs @@ -829,7 +829,7 @@ impl EntityView { pub fn get_pair_first(&self) -> Option<&First> where First: CachedComponentData + ComponentType + NotEmptyComponent, - Second: CachedComponentData + ComponentType + EmptyComponent, + Second: CachedComponentData + ComponentType, { self.get_pair_first_id(Second::get_id(self.world)) } diff --git a/flecs_ecs/src/core/world.rs b/flecs_ecs/src/core/world.rs index 5edacf9e..f0484b1b 100644 --- a/flecs_ecs/src/core/world.rs +++ b/flecs_ecs/src/core/world.rs @@ -920,7 +920,7 @@ impl World { pub fn set_pair_first(&self, first: First) where First: CachedComponentData + ComponentType + NotEmptyComponent, - Second: CachedComponentData + ComponentType + EmptyComponent, + Second: CachedComponentData + ComponentType, { let entity = Entity::new_from_existing_raw(self.raw_world, First::get_id(self.raw_world)); entity.set_pair_first::(first);