Skip to content

Commit

Permalink
#1367 Fix issue with batched OnAdd and OnRemove observers
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens authored Sep 21, 2024
1 parent 3095237 commit b92cf06
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
4 changes: 2 additions & 2 deletions distr/flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -10006,13 +10006,13 @@ void flecs_cmd_batch_for_entity(
} while ((cur = next_for_entity));
}

if (added.count) {
if (added.count && r->table) {
ecs_table_diff_t add_diff = ECS_TABLE_DIFF_INIT;
add_diff.added = added;
add_diff.added_flags = diff->added_flags;

flecs_defer_begin(world, world->stages[0]);
flecs_notify_on_add(world, table, start_table,
flecs_notify_on_add(world, r->table, start_table,
ECS_RECORD_TO_ROW(r->row), 1, &add_diff, 0, set_mask, true, false);
flecs_defer_end(world, world->stages[0]);
if (r->row & EcsEntityIsTraversable) {
Expand Down
4 changes: 2 additions & 2 deletions src/entity.c
Original file line number Diff line number Diff line change
Expand Up @@ -5018,13 +5018,13 @@ void flecs_cmd_batch_for_entity(
} while ((cur = next_for_entity));
}

if (added.count) {
if (added.count && r->table) {
ecs_table_diff_t add_diff = ECS_TABLE_DIFF_INIT;
add_diff.added = added;
add_diff.added_flags = diff->added_flags;

flecs_defer_begin(world, world->stages[0]);
flecs_notify_on_add(world, table, start_table,
flecs_notify_on_add(world, r->table, start_table,
ECS_RECORD_TO_ROW(r->row), 1, &add_diff, 0, set_mask, true, false);
flecs_defer_end(world, world->stages[0]);
if (r->row & EcsEntityIsTraversable) {
Expand Down
3 changes: 2 additions & 1 deletion test/core/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -2172,7 +2172,8 @@
"add_isa_set_w_override_batched",
"add_set_isa_w_override_batched",
"add_batched_set_with",
"defer_emplace_after_remove"
"defer_emplace_after_remove",
"batched_w_table_change_in_observer"
]
}, {
"id": "SingleThreadStaging",
Expand Down
41 changes: 41 additions & 0 deletions test/core/src/Commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -4149,3 +4149,44 @@ void Commands_defer_emplace_after_remove(void) {

ecs_fini(world);
}

static
void RemoveVelocity(ecs_iter_t *it) {
test_int(it->count, 1);
ecs_remove(it->world, it->entities[0], Velocity);
}

void Commands_batched_w_table_change_in_observer(void) {
ecs_world_t *world = ecs_mini();

ECS_COMPONENT_DEFINE(world, Position);
ECS_COMPONENT_DEFINE(world, Velocity);
ECS_TAG(world, Foo);

ecs_observer(world, {
.query.terms = {{ ecs_id(Position) }},
.events = { EcsOnRemove },
.callback = RemoveVelocity
});

ecs_observer(world, {
.query.terms = {{ Foo }},
.events = { EcsOnAdd },
.callback = DummyObserver
});

ecs_entity_t e1 = ecs_new(world);
ecs_add(world, e1, Position);
ecs_add(world, e1, Velocity);

ecs_defer_begin(world);
ecs_remove(world, e1, Position);
ecs_add(world, e1, Foo);
ecs_defer_end(world);

test_assert(ecs_has(world, e1, Foo));
test_assert(!ecs_has(world, e1, Position));
test_assert(!ecs_has(world, e1, Velocity));

ecs_fini(world);
}
7 changes: 6 additions & 1 deletion test/core/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2092,6 +2092,7 @@ void Commands_add_isa_set_w_override_batched(void);
void Commands_add_set_isa_w_override_batched(void);
void Commands_add_batched_set_with(void);
void Commands_defer_emplace_after_remove(void);
void Commands_batched_w_table_change_in_observer(void);

// Testsuite 'SingleThreadStaging'
void SingleThreadStaging_setup(void);
Expand Down Expand Up @@ -10349,6 +10350,10 @@ bake_test_case Commands_testcases[] = {
{
"defer_emplace_after_remove",
Commands_defer_emplace_after_remove
},
{
"batched_w_table_change_in_observer",
Commands_batched_w_table_change_in_observer
}
};

Expand Down Expand Up @@ -11205,7 +11210,7 @@ static bake_test_suite suites[] = {
"Commands",
NULL,
NULL,
139,
140,
Commands_testcases
},
{
Expand Down

0 comments on commit b92cf06

Please sign in to comment.