Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Querying for flattened relationship #967

Open
Tisten opened this issue Apr 21, 2023 · 4 comments
Open

Querying for flattened relationship #967

Tisten opened this issue Apr 21, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@Tisten
Copy link

Tisten commented Apr 21, 2023

Describe the bug
The flatten function sets invalid ChildOf relationships, so when using ecs_children() of the root after flattening we get all entities in the world.

To Reproduce
I wrote a test for it:

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

    ecs_entity_t p = ecs_new_id(world);
    
    ecs_entity_t p_1 = ecs_new_w_pair(world, EcsChildOf, p);
    ecs_entity_t p_1_1 = ecs_new_w_pair(world, EcsChildOf, p_1);

    ecs_entity_t p_2 = ecs_new_w_pair(world, EcsChildOf, p);
    ecs_entity_t p_2_1 = ecs_new_w_pair(world, EcsChildOf, p_2);
	
    ecs_iter_t it = ecs_children(world, p);
    test_bool(true, ecs_children_next(&it));
    test_int(it.count, 2);
    test_uint(p_1, it.entities[0]);
    test_uint(p_2, it.entities[1]);
    test_bool(false, ecs_children_next(&it));

    ecs_flatten(world, ecs_pair(EcsChildOf, p), NULL);
	
    it = ecs_children(world, p);
    test_bool(true, ecs_children_next(&it));
    test_int(it.count, 2);
    test_uint(p_1, it.entities[0]);
    test_uint(p_2, it.entities[1]);
    // This is what we currently get. p_1_1 and p_2_1 now have (ChildOf, p) set.
    //test_bool(true, ecs_children_next(&it));
    //test_int(it.count, 2);
    //test_uint(p_1_1, it.entities[0]);
    //test_uint(p_2_1, it.entities[1]);
    test_bool(false, ecs_children_next(&it));

    ecs_fini(world);
}
@Tisten Tisten added the bug Something isn't working label Apr 21, 2023
@SanderMertens SanderMertens added enhancement New feature or request and removed bug Something isn't working labels Apr 24, 2023
@SanderMertens
Copy link
Owner

Iterating using a flattened relationship is currently only supported by queries. Support for filters (which is what ecs_children uses under the hood) is planned, but may take a while to get implemented.

@Tisten
Copy link
Author

Tisten commented Apr 24, 2023

Okay.

Are there any other known limitations for flattening than that filters is not supported, plus the things documented at https://www.flecs.dev/flecs/group__entity__info.html#gaa15f32a7016cfe84291207e209ba7a1a

Could you guess at an ETA based on how you prioritize this filter work? Perhaps done Q2 this year? Or before end of year?

Also, in what way is the commit above related to this issue?

@Tisten
Copy link
Author

Tisten commented Apr 24, 2023

I reimplemented the test using a query instead and got the same results. Are queries using filters when iterating over their results? Or how would I write a query which works?
Test code:

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

    ECS_ENTITY(world, P, 0);
    ECS_ENTITY(world, P_1, (ChildOf, P));
    ECS_ENTITY(world, P_1_1, (ChildOf, P.P_1));
    ECS_ENTITY(world, P_2, (ChildOf, P));
    ECS_ENTITY(world, P_2_1, (ChildOf, P.P_2));
	
    ecs_query_t *q = ecs_query_new(world, "(ChildOf, P)");
    ecs_iter_t it = ecs_query_iter(world, q);
    test_bool(true, ecs_query_next(&it));
    test_int(it.count, 2);
    test_uint(P_1, it.entities[0]);
    test_uint(P_2, it.entities[1]);
    test_bool(false, ecs_query_next(&it));

    ecs_flatten(world, ecs_pair(EcsChildOf, P), NULL);
	
    it = ecs_query_iter(world, q);
    test_bool(true, ecs_query_next(&it));
    test_int(it.count, 2);
    test_uint(P_1, it.entities[0]);
    test_uint(P_2, it.entities[1]);
    // This is what we currently get. p_1_1 and p_2_1 now have (ChildOf, p) set.
    //test_bool(true, ecs_query_next(&it));
    //test_int(it.count, 2);
    //test_uint(P_1_1, it.entities[0]);
    //test_uint(P_2_1, it.entities[1]);
    test_bool(false, ecs_query_next(&it));

    ecs_fini(world);
}

@SanderMertens SanderMertens changed the title ecs_flatten() sets invalid ChildOf relationships Querying for flattened relationship Apr 24, 2023
@SanderMertens
Copy link
Owner

Yeah the challenge is that in a flattened tree the index that stores where the children of a parent are located no longer exists. To make querying for children of a specific flattened parent work will require a new data structure that's stored alongside the flattened tree.

I'll have to think a bit about what this would look like. One of the benefits of a flattened tree is that it can significantly reduce RAM utilization because the lookup indices are no longer stored for each flattened parent, I don't want to undo that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants