You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
When calling entity_builder::emplace<T>, the on_add hook is not called. The documentation states Emplace may only be called for components that have not yet been added to the entity, so my assumption is that emplacing should trigger the on_add hook. Emplace does trigger the on_set hook. When emplacing while deferred, it does trigger the on_add hook as well as the on_set when deferral ends, but I think that's because of the issue in #784.
The on_set hook being called makes it seem like this is intended behaviour, however the documentation saying emplace can only be called on entities without the component makes it seem like on_add should be called instead. ecs_type_hooks_t makes no mention of which ones are called on emplace, nor does ecs_emplace_id, only that it can only be called on entities without the component.
To Reproduce
#include"flecs.h"
#include<iostream>structTestStruct
{
int i_ = 0;
TestStruct() = default;
TestStruct(int i) : i_(i) {}
};
intmain()
{
flecs::world ecs;
ecs.component<TestStruct>()
.on_add([](flecs::entity e, TestStruct& t) {
std::cout << "Entity '" << e.name() << "' has value on_add hook '" << t.i_ << "'\n";
})
.on_set([](flecs::entity e, TestStruct& t) {
std::cout << "Entity '" << e.name() << "' has value on_set hook '" << t.i_ << "'\n";
});
// ecs.defer_begin(); // 1autoe1 = ecs.entity("e1").add<TestStruct>();
autoe2 = ecs.entity("e2").emplace<TestStruct>(1);
// ecs.defer_end(); // 2auto e2_v = e2.get<TestStruct>();
std::cout << "Entity '" << e2.name() << "' has value after '" << e2_v->i_ << "'\n";
}
Output of the above program is:
Entity 'e1' has value on_add hook '0'
Entity 'e2' has value on_set hook '1'
Entity 'e2' has value after '1'
With lines 1 and 2 uncommented, output is:
Entity 'e1' has value on_add hook '0'
Entity 'e2' has value on_add hook '0'
Entity 'e2' has value on_set hook '1'
Entity 'e2' has value after '1'
Expected behavior
Expected output:
Entity 'e1' has value on_add hook '0'
Entity 'e2' has value on_add hook '1'
Entity 'e2' has value after '1'
Additional context
Ubuntu 22.04, gcc-12 to compile flecs.c, g++-12 to compile the above.
The text was updated successfully, but these errors were encountered:
One thing to keep in mind is that on_add hooks are called before the component value is assigned (either with emplace or set). This means reading/writing the value of a component in an on_add hook that does not have a default constructor is UB.
Describe the bug
When calling
entity_builder::emplace<T>
, theon_add
hook is not called. The documentation statesEmplace may only be called for components that have not yet been added to the entity
, so my assumption is that emplacing should trigger theon_add
hook. Emplace does trigger theon_set
hook. When emplacing while deferred, it does trigger theon_add
hook as well as theon_set
when deferral ends, but I think that's because of the issue in #784.The
on_set
hook being called makes it seem like this is intended behaviour, however the documentation sayingemplace
can only be called on entities without the component makes it seem likeon_add
should be called instead.ecs_type_hooks_t
makes no mention of which ones are called onemplace
, nor doesecs_emplace_id
, only that it can only be called on entities without the component.To Reproduce
Output of the above program is:
With lines 1 and 2 uncommented, output is:
Expected behavior
Expected output:
Additional context
Ubuntu 22.04,
gcc-12
to compileflecs.c
,g++-12
to compile the above.The text was updated successfully, but these errors were encountered: