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

Fix C++ API observers with sparse components by filling out iterator field flags in flecs_uni_observer_invoke #1299

Merged
merged 2 commits into from
Aug 12, 2024

Conversation

Megafunk
Copy link
Contributor

I noticed C++ observers with sparse components cause this assert to fire:

fatal: flecs.c: 10947: assert: !(idr->flags & EcsIdIsSparse) use ecs_field_at to access fields for sparse components (INVALID_OPERATION)

A simple repro:

#include "flecs.h"
#include <iostream>

int main(int, char *[])
{
    struct Position {
        double x, y;
    };
    
    flecs::world ecs;
    ecs.component<Position>().add(flecs::Sparse);

    ecs.observer<Position>().event(flecs::OnSet).each([](flecs::entity e, Position& Pos)
    {
       std::cout << " - Position: {" << Pos.x << ", " << Pos.y << "}\n";
    });

    for (int i = 0; i < 4; ++i)
    {
        flecs::entity MeshEntity1 = ecs.entity();
        MeshEntity1.set<Position>({1,2});
    }
    
    ecs.progress();
}

After some messing around I found that each_delegate::invoke(ecs_iter_t *iter) seems to expect ref_fields to be set to determine if it uses terms.populate rather than terms.populate_self. I am not sure if this is the correct fix as this is the first time I've really done anything in flecs internals.

I think it would be useful to run the test cases with different combinations of sparse and non-sparse components to catch things like this ahead of time for commonly used API things.

…lecs_uni_observer_invoke to fix C++ api observers with sparse components
@Megafunk Megafunk changed the title Fix C++ sparse OnSet observers by filling out iterator field flags Fix C++ API observers with sparse components by filling out iterator field flags Aug 11, 2024
@Megafunk Megafunk changed the title Fix C++ API observers with sparse components by filling out iterator field flags Fix C++ API observers with sparse components by filling out iterator field flags in flecs_uni_observer_invoke Aug 11, 2024
@SanderMertens
Copy link
Owner

Nice! Can you add a test case for the issue as well?

@Megafunk
Copy link
Contributor Author

I added a single test for sparse on set observers based on the others in the file

@SanderMertens
Copy link
Owner

Thanks for the PR!

@SanderMertens SanderMertens merged commit a9ade24 into SanderMertens:master Aug 12, 2024
70 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants