diff --git a/distr/flecs.c b/distr/flecs.c index a277b96272..c34d8c4b44 100644 --- a/distr/flecs.c +++ b/distr/flecs.c @@ -8014,10 +8014,11 @@ ecs_record_t* flecs_new_entity( ecs_flags32_t evt_flags) { ecs_assert(r != NULL, ECS_INTERNAL_ERROR, NULL); - int32_t row = ecs_table_count(table); + r->table = &world->store.root; + flecs_table_append(world, table, entity, ctor, true); + int32_t row = ecs_table_count(table) - 1; r->table = table; r->row = ECS_ROW_TO_RECORD(row, r->row & ECS_ROW_FLAGS_MASK); - flecs_table_append(world, table, entity, ctor, true); ecs_assert(ecs_table_count(table) > row, ECS_INTERNAL_ERROR, NULL); flecs_actions_new(world, table, row, 1, diff, evt_flags, ctor, true); @@ -8056,14 +8057,12 @@ void flecs_move_entity( /* Invoke remove actions for removed components */ flecs_actions_move_remove(world, src_table, dst_table, src_row, 1, diff); - record->table = dst_table; - record->row = ECS_ROW_TO_RECORD(dst_row, record->row & ECS_ROW_FLAGS_MASK); - /* Copy entity & components from src_table to dst_table */ flecs_table_move(world, entity, entity, dst_table, dst_row, src_table, src_row, ctor); - ecs_assert(record->table == dst_table, ECS_INTERNAL_ERROR, NULL); - + record->table = dst_table; + record->row = ECS_ROW_TO_RECORD(dst_row, record->row & ECS_ROW_FLAGS_MASK); + flecs_table_delete(world, src_table, src_row, false); flecs_actions_move_add(world, dst_table, src_table, dst_row, 1, diff, diff --git a/src/entity.c b/src/entity.c index 03199cef83..c486ff8458 100644 --- a/src/entity.c +++ b/src/entity.c @@ -171,10 +171,11 @@ ecs_record_t* flecs_new_entity( ecs_flags32_t evt_flags) { ecs_assert(r != NULL, ECS_INTERNAL_ERROR, NULL); - int32_t row = ecs_table_count(table); + r->table = &world->store.root; + flecs_table_append(world, table, entity, ctor, true); + int32_t row = ecs_table_count(table) - 1; r->table = table; r->row = ECS_ROW_TO_RECORD(row, r->row & ECS_ROW_FLAGS_MASK); - flecs_table_append(world, table, entity, ctor, true); ecs_assert(ecs_table_count(table) > row, ECS_INTERNAL_ERROR, NULL); flecs_actions_new(world, table, row, 1, diff, evt_flags, ctor, true); @@ -213,14 +214,12 @@ void flecs_move_entity( /* Invoke remove actions for removed components */ flecs_actions_move_remove(world, src_table, dst_table, src_row, 1, diff); - record->table = dst_table; - record->row = ECS_ROW_TO_RECORD(dst_row, record->row & ECS_ROW_FLAGS_MASK); - /* Copy entity & components from src_table to dst_table */ flecs_table_move(world, entity, entity, dst_table, dst_row, src_table, src_row, ctor); - ecs_assert(record->table == dst_table, ECS_INTERNAL_ERROR, NULL); - + record->table = dst_table; + record->row = ECS_ROW_TO_RECORD(dst_row, record->row & ECS_ROW_FLAGS_MASK); + flecs_table_delete(world, src_table, src_row, false); flecs_actions_move_add(world, dst_table, src_table, dst_row, 1, diff, diff --git a/test/core/project.json b/test/core/project.json index fbdadd396c..d5f453d7cf 100644 --- a/test/core/project.json +++ b/test/core/project.json @@ -982,7 +982,6 @@ "delete_mixed_tree_3", "delete_mixed_tree_4", "delete_mixed_tree_5", - "instantiate_parent_w_has_in_hook", "add_prefab_tag_after_hierarchy_creation", "defer_add_prefab_tag_after_hierarchy_creation", "add_prefab_tag_after_hierarchy_creation_2", @@ -1713,7 +1712,8 @@ "has_in_on_add_hook_new", "has_in_on_add_hook_move", "get_in_on_add_hook_new", - "get_in_on_add_hook_move" + "get_in_on_add_hook_move", + "get_name_in_on_add_hook_move" ] }, { "id": "Pairs", diff --git a/test/core/src/ComponentLifecycle.c b/test/core/src/ComponentLifecycle.c index a1eba5b498..1c15520f42 100644 --- a/test/core/src/ComponentLifecycle.c +++ b/test/core/src/ComponentLifecycle.c @@ -4418,7 +4418,7 @@ static void HasHook(ecs_iter_t *it) { for (int i = 0; i < it->count; i ++) { ecs_entity_t e = it->entities[i]; - test_assert(ecs_has(world, e, Position)); + test_assert(!ecs_has(world, e, Position)); has_hook_invoked ++; } } @@ -4431,13 +4431,12 @@ static void GetHook(ecs_iter_t *it) { for (int i = 0; i < it->count; i ++) { ecs_entity_t e = it->entities[i]; - test_assert(ecs_has(world, e, Position)); - test_assert(ecs_get(world, e, Position) == &p[i]); + test_assert(!ecs_has(world, e, Position)); + test_assert(ecs_get(world, e, Position) == NULL); get_hook_invoked ++; } } - void ComponentLifecycle_has_in_on_add_hook_new(void) { ecs_world_t *world = ecs_mini(); @@ -4513,3 +4512,37 @@ void ComponentLifecycle_get_in_on_add_hook_move(void) { ecs_fini(world); } + +static int get_name_hook_invoked = 0; + +static void GetNameHook(ecs_iter_t *it) { + ecs_world_t *world = it->world; + + for (int i = 0; i < it->count; i ++) { + ecs_entity_t e = it->entities[i]; + const char *name = ecs_get_name(world, e); + test_assert(name != NULL); + test_str(name, "TestEntity"); + get_name_hook_invoked ++; + } +} + +void ComponentLifecycle_get_name_in_on_add_hook_move(void) { + ecs_world_t *world = ecs_mini(); + + ECS_COMPONENT_DEFINE(world, Position); + + ecs_set_hooks(world, Position, { + .on_add = GetNameHook + }); + + ecs_entity_t e = ecs_entity(world, { .name = "TestEntity" }); + test_int(get_name_hook_invoked, 0); + + ecs_add(world, e, Position); + test_int(get_name_hook_invoked, 1); + + test_str(ecs_get_name(world, e), "TestEntity"); + + ecs_fini(world); +} diff --git a/test/core/src/NonFragmentingChildOf.c b/test/core/src/NonFragmentingChildOf.c index 507919d588..ccdbaccf2e 100644 --- a/test/core/src/NonFragmentingChildOf.c +++ b/test/core/src/NonFragmentingChildOf.c @@ -6055,43 +6055,6 @@ void NonFragmentingChildOf_delete_mixed_tree_5(void) { ecs_fini(world); } -static int dummy_hook_invoked = 0; - -static void DummyHook(ecs_iter_t *it) { - ecs_world_t *world = it->world; - Position *p = ecs_field(it, Position, 0); - - for (int i = 0; i < it->count; i ++) { - ecs_entity_t e = it->entities[i]; - test_assert(ecs_has(world, e, Position)); - test_assert(ecs_get(world, e, Position) == &p[i]); - dummy_hook_invoked ++; - } -} - -void NonFragmentingChildOf_instantiate_parent_w_has_in_hook(void) { - ecs_world_t* world = ecs_mini(); - - ECS_COMPONENT_DEFINE(world, Position); - - ecs_set_hooks(world, Position, { - .on_add = DummyHook - }); - - ecs_entity_t prefab = ecs_new_w_id(world, EcsPrefab); - ecs_entity_t prefab_child = ecs_new_w_parent(world, prefab, NULL); - ecs_set(world, prefab_child, Position, {10, 20}); - test_int(dummy_hook_invoked, 1); - - ecs_new_w_pair(world, EcsIsA, prefab); - test_int(dummy_hook_invoked, 2); - - ecs_new_w_pair(world, EcsIsA, prefab); - test_int(dummy_hook_invoked, 3); - - ecs_fini(world); -} - void NonFragmentingChildOf_add_prefab_tag_after_hierarchy_creation(void) { ecs_world_t* world = ecs_mini(); diff --git a/test/core/src/main.c b/test/core/src/main.c index 0a45d61c5a..38603fb8a3 100644 --- a/test/core/src/main.c +++ b/test/core/src/main.c @@ -950,7 +950,6 @@ void NonFragmentingChildOf_delete_mixed_tree_2(void); void NonFragmentingChildOf_delete_mixed_tree_3(void); void NonFragmentingChildOf_delete_mixed_tree_4(void); void NonFragmentingChildOf_delete_mixed_tree_5(void); -void NonFragmentingChildOf_instantiate_parent_w_has_in_hook(void); void NonFragmentingChildOf_add_prefab_tag_after_hierarchy_creation(void); void NonFragmentingChildOf_defer_add_prefab_tag_after_hierarchy_creation(void); void NonFragmentingChildOf_add_prefab_tag_after_hierarchy_creation_2(void); @@ -1653,6 +1652,7 @@ void ComponentLifecycle_has_in_on_add_hook_new(void); void ComponentLifecycle_has_in_on_add_hook_move(void); void ComponentLifecycle_get_in_on_add_hook_new(void); void ComponentLifecycle_get_in_on_add_hook_move(void); +void ComponentLifecycle_get_name_in_on_add_hook_move(void); // Testsuite 'Pairs' void Pairs_type_w_one_pair(void); @@ -6937,10 +6937,6 @@ bake_test_case NonFragmentingChildOf_testcases[] = { "delete_mixed_tree_5", NonFragmentingChildOf_delete_mixed_tree_5 }, - { - "instantiate_parent_w_has_in_hook", - NonFragmentingChildOf_instantiate_parent_w_has_in_hook - }, { "add_prefab_tag_after_hierarchy_creation", NonFragmentingChildOf_add_prefab_tag_after_hierarchy_creation @@ -9654,6 +9650,10 @@ bake_test_case ComponentLifecycle_testcases[] = { { "get_in_on_add_hook_move", ComponentLifecycle_get_in_on_add_hook_move + }, + { + "get_name_in_on_add_hook_move", + ComponentLifecycle_get_name_in_on_add_hook_move } }; @@ -15960,7 +15960,7 @@ static bake_test_suite suites[] = { "NonFragmentingChildOf", NULL, NULL, - 239, + 238, NonFragmentingChildOf_testcases }, { @@ -16058,7 +16058,7 @@ static bake_test_suite suites[] = { "ComponentLifecycle", ComponentLifecycle_setup, NULL, - 138, + 139, ComponentLifecycle_testcases }, {