Skip to content

Commit

Permalink
#1513 Fix issues with assigning flecs::Component to implicitly regist…
Browse files Browse the repository at this point in the history
…ered tags and prefabs
  • Loading branch information
SanderMertens authored Jan 10, 2025
1 parent fbb7c0c commit 1894342
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 9 deletions.
6 changes: 3 additions & 3 deletions distr/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -27401,7 +27401,7 @@ struct type_impl {
// Register component id.
static entity_t register_id(world_t *world,
const char *name = nullptr, bool allow_tag = true, flecs::id_t id = 0,
bool is_component = false, bool implicit_name = true, const char *n = nullptr,
bool is_component = true, bool implicit_name = true, const char *n = nullptr,
flecs::entity_t module = 0)
{
if (!s_index) {
Expand Down Expand Up @@ -29348,7 +29348,7 @@ inline flecs::entity world::entity(E value) const {

template <typename T>
inline flecs::entity world::entity(const char *name) const {
return flecs::entity(world_, _::type<T>::register_id(world_, name, true) );
return flecs::entity(world_, _::type<T>::register_id(world_, name, true, 0, false) );
}

template <typename... Args>
Expand All @@ -29360,7 +29360,7 @@ inline flecs::entity world::prefab(Args &&... args) const {

template <typename T>
inline flecs::entity world::prefab(const char *name) const {
flecs::entity result = flecs::component<T>(world_, name, true);
flecs::entity result = this->entity<T>(name);
result.add(flecs::Prefab);
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion include/flecs/addons/cpp/component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ struct type_impl {
// Register component id.
static entity_t register_id(world_t *world,
const char *name = nullptr, bool allow_tag = true, flecs::id_t id = 0,
bool is_component = false, bool implicit_name = true, const char *n = nullptr,
bool is_component = true, bool implicit_name = true, const char *n = nullptr,
flecs::entity_t module = 0)
{
if (!s_index) {
Expand Down
4 changes: 2 additions & 2 deletions include/flecs/addons/cpp/mixins/entity/impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ inline flecs::entity world::entity(E value) const {

template <typename T>
inline flecs::entity world::entity(const char *name) const {
return flecs::entity(world_, _::type<T>::register_id(world_, name, true) );
return flecs::entity(world_, _::type<T>::register_id(world_, name, true, 0, false) );
}

template <typename... Args>
Expand All @@ -232,7 +232,7 @@ inline flecs::entity world::prefab(Args &&... args) const {

template <typename T>
inline flecs::entity world::prefab(const char *name) const {
flecs::entity result = flecs::component<T>(world_, name, true);
flecs::entity result = this->entity<T>(name);
result.add(flecs::Prefab);
return result;
}
Expand Down
5 changes: 4 additions & 1 deletion test/cpp/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@
"entity_w_type",
"entity_w_nested_type",
"entity_w_type_defer",
"prefab_w_type",
"prefab_hierarchy_w_types",
"prefab_hierarchy_w_root_types",
"prefab_hierarchy_w_child_override",
Expand Down Expand Up @@ -1156,7 +1157,9 @@
"implicit_const",
"implicit_ref",
"implicit_const_ref",
"vector_elem_type"
"vector_elem_type",
"tag_has_component",
"component_has_component"
]
}, {
"id": "WorldFactory",
Expand Down
15 changes: 15 additions & 0 deletions test/cpp/src/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3998,6 +3998,20 @@ void Entity_entity_w_type(void) {
test_assert(e == e_2);
}

void Entity_prefab_w_type(void) {
flecs::world ecs;

auto e = ecs.prefab<EntityType>();

test_str(e.name().c_str(), "EntityType");
test_str(e.path().c_str(), "::EntityType");
test_assert(!e.has<flecs::Component>());
test_assert(e.has(flecs::Prefab));

auto e_2 = ecs.entity<EntityType>();
test_assert(e == e_2);
}

struct Turret {
struct Base { };
};
Expand Down Expand Up @@ -4903,3 +4917,4 @@ void Entity_iter_empty_type(void) {

test_int(count, 0);
}

16 changes: 16 additions & 0 deletions test/cpp/src/ImplicitComponents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,3 +470,19 @@ void ImplicitComponents_vector_elem_type(void) {
test_assert(v != 0);
}
}

struct EmptyType { };

void ImplicitComponents_tag_has_component(void) {
flecs::world world;

flecs::id c = world.id<EmptyType>();
test_assert(c.entity().has<flecs::Component>());
}

void ImplicitComponents_component_has_component(void) {
flecs::world world;

flecs::id c = world.id<Position>();
test_assert(c.entity().has<flecs::Component>());
}
19 changes: 17 additions & 2 deletions test/cpp/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ void Entity_entity_w_root_name_from_scope(void);
void Entity_entity_w_type(void);
void Entity_entity_w_nested_type(void);
void Entity_entity_w_type_defer(void);
void Entity_prefab_w_type(void);
void Entity_prefab_hierarchy_w_types(void);
void Entity_prefab_hierarchy_w_root_types(void);
void Entity_prefab_hierarchy_w_child_override(void);
Expand Down Expand Up @@ -1116,6 +1117,8 @@ void ImplicitComponents_implicit_const(void);
void ImplicitComponents_implicit_ref(void);
void ImplicitComponents_implicit_const_ref(void);
void ImplicitComponents_vector_elem_type(void);
void ImplicitComponents_tag_has_component(void);
void ImplicitComponents_component_has_component(void);

// Testsuite 'WorldFactory'
void WorldFactory_entity(void);
Expand Down Expand Up @@ -2327,6 +2330,10 @@ bake_test_case Entity_testcases[] = {
"entity_w_type_defer",
Entity_entity_w_type_defer
},
{
"prefab_w_type",
Entity_prefab_w_type
},
{
"prefab_hierarchy_w_types",
Entity_prefab_hierarchy_w_types
Expand Down Expand Up @@ -5775,6 +5782,14 @@ bake_test_case ImplicitComponents_testcases[] = {
{
"vector_elem_type",
ImplicitComponents_vector_elem_type
},
{
"tag_has_component",
ImplicitComponents_tag_has_component
},
{
"component_has_component",
ImplicitComponents_component_has_component
}
};

Expand Down Expand Up @@ -7012,7 +7027,7 @@ static bake_test_suite suites[] = {
"Entity",
NULL,
NULL,
278,
279,
Entity_testcases
},
{
Expand Down Expand Up @@ -7119,7 +7134,7 @@ static bake_test_suite suites[] = {
"ImplicitComponents",
NULL,
NULL,
27,
29,
ImplicitComponents_testcases
},
{
Expand Down

0 comments on commit 1894342

Please sign in to comment.