diff --git a/flecs.c b/flecs.c index 757de687a..bcd77410d 100644 --- a/flecs.c +++ b/flecs.c @@ -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; @@ -8684,6 +8693,8 @@ ecs_entity_t ecs_get_target_for_id( } else { return subject; } +error: + return 0; } int32_t ecs_get_depth( diff --git a/src/entity.c b/src/entity.c index 286db2ad1..4320d5038 100644 --- a/src/entity.c +++ b/src/entity.c @@ -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; @@ -3308,6 +3317,8 @@ ecs_entity_t ecs_get_target_for_id( } else { return subject; } +error: + return 0; } int32_t ecs_get_depth( diff --git a/test/api/project.json b/test/api/project.json index ce6838488..25720f633 100644 --- a/test/api/project.json +++ b/test/api/project.json @@ -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", diff --git a/test/api/src/Pairs.c b/test/api/src/Pairs.c index d3de7228a..888a61fa6 100644 --- a/test/api/src/Pairs.c +++ b/test/api/src/Pairs.c @@ -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(); diff --git a/test/api/src/main.c b/test/api/src/main.c index c665c5877..cfaf67cef 100644 --- a/test/api/src/main.c +++ b/test/api/src/main.c @@ -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); @@ -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 @@ -12133,7 +12143,7 @@ static bake_test_suite suites[] = { "Pairs", NULL, NULL, - 108, + 110, Pairs_testcases }, {