SanderMertens / flecs

A fast entity component system (ECS) for C & C++

Home Page:https://www.flecs.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

data is not ensured when triggering OnAdd system or observer.

eziasolsky opened this issue · comments

Describe the bug
data is not ensured when triggering OnAdd system or observer.

To Reproduce
with flecs 2.4.8, the following is the modified "example cpp simple system".
`
#include <simple_system.h>
#include

struct Message {
int v;
};

int main(int argc, char* argv[]) {
flecs::world ecs(argc, argv);

ecs.system<Message>()
	.kind(flecs::OnAdd)
	.each([](Message& messages) {
	std::cout << messages.v << std::endl;
		});

flecs::entity Apple = ecs.entity().set<Message>({ 1 });
Apple.destruct();
flecs::entity Orange = ecs.entity().set<Message>({ 2 });

return 0;

}
`

Expected behavior
expects
1 2
but got
-842150451 1

This is expected behavior, as the OnAdd observer is invoked before the component value is assigned. If you need to access the component value that is assigned by set, change the event to flecs::OnSet.