Skip to content

Commit

Permalink
Fix issue that prevented using ecs_get_target_for_id from stage
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Mar 21, 2023
1 parent 439423f commit 0ba0fdc
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
11 changes: 11 additions & 0 deletions flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -8648,6 +8648,15 @@ ecs_entity_t ecs_get_target_for_id(
ecs_entity_t rel,
ecs_id_t id)
{
ecs_check(world != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_check(ecs_is_alive(world, entity), ECS_INVALID_PARAMETER, NULL);

if (!id) {
return ecs_get_target(world, entity, rel, 0);
}

world = ecs_get_world(world);

ecs_table_t *table = ecs_get_table(world, entity);
ecs_entity_t subject = 0;

Expand Down Expand Up @@ -8684,6 +8693,8 @@ ecs_entity_t ecs_get_target_for_id(
} else {
return subject;
}
error:
return 0;
}

int32_t ecs_get_depth(
Expand Down
11 changes: 11 additions & 0 deletions src/entity.c
Original file line number Diff line number Diff line change
Expand Up @@ -3272,6 +3272,15 @@ ecs_entity_t ecs_get_target_for_id(
ecs_entity_t rel,
ecs_id_t id)
{
ecs_check(world != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_check(ecs_is_alive(world, entity), ECS_INVALID_PARAMETER, NULL);

if (!id) {
return ecs_get_target(world, entity, rel, 0);
}

world = ecs_get_world(world);

ecs_table_t *table = ecs_get_table(world, entity);
ecs_entity_t subject = 0;

Expand Down Expand Up @@ -3308,6 +3317,8 @@ ecs_entity_t ecs_get_target_for_id(
} else {
return subject;
}
error:
return 0;
}

int32_t ecs_get_depth(
Expand Down
2 changes: 2 additions & 0 deletions test/api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -1649,6 +1649,8 @@
"get_target_for_id_from_empty",
"get_target_for_id_from_empty_no_rel",
"get_target_for_id_not_empty_not_found",
"get_target_for_id_from_stage",
"get_target_for_id_no_id",
"ignore_childof_from_base",
"add_exclusive_relation_twice",
"add_same_exclusive_relation_twice",
Expand Down
34 changes: 34 additions & 0 deletions test/api/src/Pairs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2255,6 +2255,40 @@ void Pairs_get_target_for_id_not_empty_not_found() {
ecs_fini(world);
}

void Pairs_get_target_for_id_from_stage() {
ecs_world_t *world = ecs_mini();

ECS_TAG(world, Tag);

ecs_entity_t base = ecs_new(world, Tag);
ecs_entity_t e = ecs_new_w_pair(world, EcsIsA, base);

ecs_world_t *stage = ecs_get_stage(world, 0);
ecs_readonly_begin(world);
ecs_entity_t result = ecs_get_target_for_id(stage, e, EcsIsA, Tag);
test_assert(result != 0);
test_assert(result == base);
ecs_readonly_end(world);

ecs_fini(world);
}

void Pairs_get_target_for_id_no_id() {
ecs_world_t *world = ecs_mini();

ECS_TAG(world, Tag);

ecs_entity_t base = ecs_new(world, Tag);
ecs_entity_t e = ecs_new_w_pair(world, EcsIsA, base);
ecs_add_id(world, e, Tag);

ecs_entity_t result = ecs_get_target_for_id(world, e, EcsIsA, 0);
test_assert(result != 0);
test_assert(result == base);

ecs_fini(world);
}

void Pairs_add_exclusive_relation_twice() {
ecs_world_t *world = ecs_mini();

Expand Down
12 changes: 11 additions & 1 deletion test/api/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,8 @@ void Pairs_get_target_for_wildcard_from_nested_base(void);
void Pairs_get_target_for_id_from_empty(void);
void Pairs_get_target_for_id_from_empty_no_rel(void);
void Pairs_get_target_for_id_not_empty_not_found(void);
void Pairs_get_target_for_id_from_stage(void);
void Pairs_get_target_for_id_no_id(void);
void Pairs_ignore_childof_from_base(void);
void Pairs_add_exclusive_relation_twice(void);
void Pairs_add_same_exclusive_relation_twice(void);
Expand Down Expand Up @@ -8574,6 +8576,14 @@ bake_test_case Pairs_testcases[] = {
"get_target_for_id_not_empty_not_found",
Pairs_get_target_for_id_not_empty_not_found
},
{
"get_target_for_id_from_stage",
Pairs_get_target_for_id_from_stage
},
{
"get_target_for_id_no_id",
Pairs_get_target_for_id_no_id
},
{
"ignore_childof_from_base",
Pairs_ignore_childof_from_base
Expand Down Expand Up @@ -12133,7 +12143,7 @@ static bake_test_suite suites[] = {
"Pairs",
NULL,
NULL,
108,
110,
Pairs_testcases
},
{
Expand Down

0 comments on commit 0ba0fdc

Please sign in to comment.