diff --git a/distr/flecs.c b/distr/flecs.c index 96f111ae4..b1936bd72 100644 --- a/distr/flecs.c +++ b/distr/flecs.c @@ -13818,6 +13818,8 @@ void flecs_emit_forward_id( return; } + ecs_entity_t old_src = it->sources[0]; + it->ids[0] = id; it->sources[0] = tgt; it->event_id = id; @@ -13884,6 +13886,8 @@ void flecs_emit_forward_id( } } + + it->sources[0] = old_src; it->up_fields = 0; } diff --git a/src/observable.c b/src/observable.c index c3dc0c51e..34e51f4e1 100644 --- a/src/observable.c +++ b/src/observable.c @@ -608,6 +608,8 @@ void flecs_emit_forward_id( return; } + ecs_entity_t old_src = it->sources[0]; + it->ids[0] = id; it->sources[0] = tgt; it->event_id = id; @@ -674,6 +676,8 @@ void flecs_emit_forward_id( } } + + it->sources[0] = old_src; it->up_fields = 0; } diff --git a/test/core/project.json b/test/core/project.json index 14b446b4e..9950061db 100644 --- a/test/core/project.json +++ b/test/core/project.json @@ -1707,6 +1707,7 @@ "on_table_create", "on_table_create_is_deferred", "on_table_create_is_deferred_batched", + "2_children_w_deferred_set", "cache_test_1", "cache_test_2", "cache_test_3", diff --git a/test/core/src/Observer.c b/test/core/src/Observer.c index bb36b8660..e0dd914d0 100644 --- a/test/core/src/Observer.c +++ b/test/core/src/Observer.c @@ -9879,3 +9879,65 @@ void Observer_on_table_create_is_deferred_batched(void) { ecs_fini(world); } + +void Observer_2_children_w_deferred_set(void) { + ecs_world_t *world = ecs_mini(); + + ECS_COMPONENT(world, Position); + + Probe ctx = {0}; + + ecs_entity_t o = ecs_observer(world, { + .query.terms = {{ ecs_id(Position) }}, + .events = { EcsOnAdd }, + .callback = Observer_w_field, + .ctx = &ctx + }); + + ecs_entity_t p = ecs_new(world); + ecs_set(world, p, Position, {10, 20}); + + test_int(ctx.invoked, 1); + test_int(ctx.count, 1); + test_int(ctx.system, o); + test_int(ctx.event, EcsOnAdd); + test_uint(ctx.e[0], p); + test_uint(ctx.s[0][0], 0); + ecs_os_zeromem(&ctx); + + { + ecs_defer_begin(world); + ecs_entity_t e = ecs_new(world); + ecs_add_pair(world, e, EcsChildOf, p); + ecs_set(world, e, Position, {20, 30}); + test_int(ctx.invoked, 0); + ecs_defer_end(world); + + test_int(ctx.invoked, 1); + test_int(ctx.count, 1); + test_int(ctx.system, o); + test_int(ctx.event, EcsOnAdd); + test_uint(ctx.e[0], e); + test_uint(ctx.s[0][0], 0); + ecs_os_zeromem(&ctx); + } + + { + ecs_defer_begin(world); + ecs_entity_t e = ecs_new(world); + ecs_add_pair(world, e, EcsChildOf, p); + ecs_set(world, e, Position, {30, 40}); + test_int(ctx.invoked, 0); + ecs_defer_end(world); + + test_int(ctx.invoked, 1); + test_int(ctx.count, 1); + test_int(ctx.system, o); + test_int(ctx.event, EcsOnAdd); + test_uint(ctx.e[0], e); + test_uint(ctx.s[0][0], 0); + ecs_os_zeromem(&ctx); + } + + ecs_fini(world); +} diff --git a/test/core/src/main.c b/test/core/src/main.c index 248ac3da6..b4a00caec 100644 --- a/test/core/src/main.c +++ b/test/core/src/main.c @@ -1646,6 +1646,7 @@ void Observer_2_up_terms_w_batched_add(void); void Observer_on_table_create(void); void Observer_on_table_create_is_deferred(void); void Observer_on_table_create_is_deferred_batched(void); +void Observer_2_children_w_deferred_set(void); void Observer_cache_test_1(void); void Observer_cache_test_2(void); void Observer_cache_test_3(void); @@ -8735,6 +8736,10 @@ bake_test_case Observer_testcases[] = { "on_table_create_is_deferred_batched", Observer_on_table_create_is_deferred_batched }, + { + "2_children_w_deferred_set", + Observer_2_children_w_deferred_set + }, { "cache_test_1", Observer_cache_test_1 @@ -11650,7 +11655,7 @@ static bake_test_suite suites[] = { "Observer", NULL, NULL, - 235, + 236, Observer_testcases }, {