Skip to content

Add/Remove entities from systems when when its component bitmask changes #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
- uses: actions/checkout@v2
- name: Build and run tests
run: |
pwd
make clean
make tests
./tests
Expand Down
22 changes: 20 additions & 2 deletions pico_ecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ void* ecs_add(ecs_t* ecs, ecs_id_t entity_id, ecs_id_t comp_id, void* args)
// belongs to
ecs_bitset_flip(&entity->comp_bits, comp_id, true);

// Add entity to systems
// Add or remove entity from systems
for (ecs_id_t sys_id = 0; sys_id < ecs->system_count; sys_id++)
{
ecs_sys_t* sys = &ecs->systems[sys_id];
Expand All @@ -922,6 +922,15 @@ void* ecs_add(ecs_t* ecs, ecs_id_t entity_id, ecs_id_t comp_id, void* args)
sys->add_cb(ecs, entity_id, sys->udata);
}
}
else // Just remove the entity if its components no longer match for whatever reason.
{
if (!ecs_bitset_is_zero(&sys->exclude_bits) &&
ecs_sparse_set_remove(&sys->entity_ids, entity_id))
{
if (sys->remove_cb)
sys->remove_cb(ecs, entity_id, sys->udata);
}
}
}

// Return component
Expand Down Expand Up @@ -956,6 +965,15 @@ void ecs_remove(ecs_t* ecs, ecs_id_t entity_id, ecs_id_t comp_id)
sys->remove_cb(ecs, entity_id, sys->udata);
}
}
else
{
if (!ecs_bitset_is_zero(&sys->exclude_bits) &&
ecs_sparse_set_add(ecs, &sys->entity_ids, entity_id))
{
if (sys->add_cb)
sys->add_cb(ecs, entity_id, sys->udata);
}
}
}

ecs_comp_t* comp = &ecs->comps[comp_id];
Expand Down Expand Up @@ -1310,7 +1328,7 @@ static size_t ecs_sparse_set_find(ecs_sparse_set_t* set, ecs_id_t id)
{
ECS_ASSERT(ecs_is_not_null(set));

if (set->sparse[id] < set->size && set->dense[set->sparse[id]] == id)
if (id < set->capacity && set->sparse[id] < set->size && set->dense[set->sparse[id]] == id)
return set->sparse[id];
else
return ECS_NULL;
Expand Down
15 changes: 15 additions & 0 deletions tests_pico_ecs/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ TEST_CASE(test_exclude)
REQUIRE(exclude_sys_state.count == 1);
REQUIRE(exclude_sys_state.eid == eid2);

// Removing comp1 from entity1 causes it to be added to the system
ecs_remove(ecs, eid1, comp1_id);
ecs_update_system(ecs, system_id, 0.0);

REQUIRE(exclude_sys_state.count == 2);
REQUIRE(exclude_sys_state.eid == eid2);

// Adding comp1 to entity2 causes it to be removed from the system
ecs_add(ecs, eid2, comp1_id, NULL);

ecs_update_system(ecs, system_id, 0.0);

REQUIRE(exclude_sys_state.count == 1);
REQUIRE(exclude_sys_state.eid == eid1);

return true;
}

Expand Down