OnSet triggered on tag relation add
Hexlord opened this issue · comments
Describe the bug
OnSet should not be triggered on relation adds (without setting data), but they do get triggered for Tag relations:
To Reproduce
Steps to reproduce the behavior:
flecs::world ECS;
auto Relation = ECS.entity().add(flecs::Tag);
ECS.observer().term(Relation, flecs::Wildcard)
.event(flecs::OnSet)
.iter([&](flecs::iter &Iter)
{ printf("Trigger!\n"); });
ECS.entity().add(Relation, ECS.entity());
Expected behavior
Nothing is printed to the output
This is actually not a bug, but the reason for this is not straightforward. What happens here is that you create an OnSet
observer for something that is guaranteed to be a tag (adding flecs::Tag
to Relation
ensures this).
Tags can't be set, so you'd intuitively expect this observer to never trigger. However, the reason it does has to do with OnSet
observers that subscribe for both components and tags:
world.observer<Position, MyTag>()
.event(flecs::OnSet)
.each( ... );
In this case the observer needs to ensure that it fires when MyTag
is added to an entity that already has Position
. To make sure that it does, the observer creates an OnAdd
trigger for the tag.
This is desired as it removes operation ordering dependencies, but in this case leads to behavior that at first glance is confusing.