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

API for traversing hierarchy depth-first #72

Closed
SanderMertens opened this issue Sep 10, 2019 · 3 comments
Closed

API for traversing hierarchy depth-first #72

SanderMertens opened this issue Sep 10, 2019 · 3 comments
Labels
enhancement New feature or request
Projects

Comments

@SanderMertens
Copy link
Owner

A common pattern for applications dealing with hierarchies is to access the data in a depth-first way. As asked by @teksoc-1 in #63 :

@SanderMertens On the subject of Flecs hierarchies, is there a public API to get all the children of a given parent? I poked around and wasn't seeing it, but seemed like a natural API to have, unless I'm missing something.

@SanderMertens
Copy link
Owner Author

@teksoc-1 Currently you can get the children for a given parent by creating an EcsManual system that matches the components of the children you want to see (e.g. TransformationMatrix), and then call ecs_run_w_filter, where you specify the parent as the filter. To get the full tree, you can invoke the system recursively.

In the next major version of Flecs this will change however, as I'm changing the way children are stored to solve some of the biggest problems that todays implementation has. There will be an API for traversing entities depth-first, but I don't know yet what it will look like.

@teksoc-1
Copy link

@SanderMertens Ahh ok, thank you for the response.

@SanderMertens SanderMertens added the enhancement New feature or request label Sep 14, 2019
@SanderMertens SanderMertens added the will take a while This will take a while label Sep 24, 2019
@SanderMertens SanderMertens added this to Todo in v2.1 Oct 7, 2019
@SanderMertens SanderMertens removed the will take a while This will take a while label May 19, 2020
@SanderMertens SanderMertens removed this from Todo in v2.1 May 19, 2020
@SanderMertens SanderMertens added this to To do in v2.0 via automation May 19, 2020
@SanderMertens SanderMertens moved this from To do to In progress in v2.0 Jun 6, 2020
@SanderMertens
Copy link
Owner Author

The API to do depth-first traversal is implemented on the v2 branch, and looks like this:

void print_tree(
    ecs_world_t *world, 
    ecs_entity_t entity) 
{
    printf("%*s%s\n", indent * 2, "", ecs_get_name(world, entity));

    indent ++;

    // Get an iterator to the children of the current entity
    ecs_view_t it = ecs_tree_iter(world, entity);

    // Iterate all the tables that contain children for the parent
    while (ecs_tree_next(&it)) {
        for (int i = 0; i < it.count; i ++) {
            // Print the child, and recursively iterate
            print_tree(world, it.entities[i]);
        }
    }

    indent --;
}

v2.0 automation moved this from In progress to Done Jun 6, 2020
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
No open projects
v2.0
  
Done
Development

No branches or pull requests

2 participants