Skip to content

Commit

Permalink
update pair functions to support 2 pairs with data + update example w…
Browse files Browse the repository at this point in the history
…/ this change.
  • Loading branch information
Indra-db committed Mar 23, 2024
1 parent a0f293e commit 031e6ae
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
50 changes: 38 additions & 12 deletions flecs_ecs/examples/relationships_component_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,22 @@ fn main() {
// Note that <Requires, Gigawatts> and <Gigawatts, Requires> 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, Position>(Expires { timeout: 0.5 });

let expires = e3.get_pair_first::<Expires, Position>();
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::<MustHave>().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, <MustHave, Position> contains no
// data because MustHave has the Tag property.
world.new_entity().add_pair::<MustHave, Position>();

// The id::type_id method can be used to find the component type for a pair:
println!(
Expand All @@ -83,6 +90,22 @@ fn main() {
.get_hierarchy_path()
.unwrap()
);
println!(
"{}",
world
.get_id_pair::<Expires, Position>()
.type_id()
.get_hierarchy_path()
.unwrap()
);
println!(
"{}",
world
.get_id_pair::<MustHave, Position>()
.type_id()
.get_hierarchy_path()
.unwrap()
);

// When querying for a relationship component, add the pair type as template
// argument to the builder:
Expand All @@ -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
}
4 changes: 2 additions & 2 deletions flecs_ecs/src/core/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ impl Entity {
pub fn set_pair_first<First, Second>(self, first: First) -> Self
where
First: CachedComponentData + ComponentType<Struct> + NotEmptyComponent,
Second: CachedComponentData + ComponentType<Struct> + EmptyComponent,
Second: CachedComponentData + ComponentType<Struct>,
{
set_helper(
self.world,
Expand Down Expand Up @@ -1928,7 +1928,7 @@ impl Entity {
pub fn get_pair_first_mut<First, Second>(&mut self) -> &mut First
where
First: CachedComponentData + ComponentType<Struct> + NotEmptyComponent,
Second: CachedComponentData + ComponentType<Struct> + EmptyComponent,
Second: CachedComponentData + ComponentType<Struct>,
{
self.get_pair_first_id_mut(Second::get_id(self.world))
}
Expand Down
2 changes: 1 addition & 1 deletion flecs_ecs/src/core/entity_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ impl EntityView {
pub fn get_pair_first<First, Second>(&self) -> Option<&First>
where
First: CachedComponentData + ComponentType<Struct> + NotEmptyComponent,
Second: CachedComponentData + ComponentType<Struct> + EmptyComponent,
Second: CachedComponentData + ComponentType<Struct>,
{
self.get_pair_first_id(Second::get_id(self.world))
}
Expand Down
2 changes: 1 addition & 1 deletion flecs_ecs/src/core/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ impl World {
pub fn set_pair_first<First, Second>(&self, first: First)
where
First: CachedComponentData + ComponentType<Struct> + NotEmptyComponent,
Second: CachedComponentData + ComponentType<Struct> + EmptyComponent,
Second: CachedComponentData + ComponentType<Struct>,
{
let entity = Entity::new_from_existing_raw(self.raw_world, First::get_id(self.raw_world));
entity.set_pair_first::<First, Second>(first);
Expand Down

0 comments on commit 031e6ae

Please sign in to comment.