SanderMertens / flecs

A fast entity component system (ECS) for C & C++
https://www.flecs.dev
Other
6.46k stars 454 forks source link

Observers don't trigger for override components #1413

Closed nimble0 closed 1 week ago

nimble0 commented 2 weeks ago

Describe the bug Observers don't trigger for override components added from a prefab. Behaviour is as expected in v3.2.11.

To Reproduce Edited flecs/examples/cpp/prefabs/src/main.cpp

#include <basics.h>
#include <iostream>

struct Defense {
    double value;
};

struct Armour {
    double value;
};

int main() {
    flecs::world ecs;

    // inherited components trigger observer
    //ecs.component<Defense>().add(flecs::OnInstantiate, flecs::Inherit);
    //ecs.component<Armour>().add(flecs::OnInstantiate, flecs::Inherit);

    ecs.observer<const Defense, Armour>("CalculateArmour")
        .event(flecs::OnSet)
        .each([&](const Defense& defense, Armour& armour)
        {
            armour.value = 1.0 / defense.value;
            std::cout << "observer trigger\n";
        });

    // Create a SpaceShip prefab with a Defense component.
    std::cout << "make prefab\n";
    flecs::entity SpaceShip = ecs.prefab("SpaceShip")
        .set<Defense>({ 50 })
        .set(Armour{});

    // Create a prefab instance
    std::cout << "make prefab instance\n";
    flecs::entity inst = ecs.entity("my_spaceship").is_a(SpaceShip);

    // calling modified triggers the observer as expected
    //inst.modified<Armour>();
}

Expected output (v3.2.11)

make prefab make prefab instance observer trigger

Actual output

make prefab make prefab instance

SanderMertens commented 1 week ago

Fixed!