Skip to content

fixes trivial removes systems not triggering an event #25

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 3 commits into from
Nov 25, 2022
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
16 changes: 15 additions & 1 deletion ecsact/entt/trivial_system_impl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ void trivial_system_impl(
EachCallbackT&& each_callback = [](auto&, auto&, auto) {}
) {
using boost::mp11::mp_for_each;
using ecsact::entt::component_removed;
using ecsact::entt::detail::pending_remove;
using ecsact::entt::detail::temp_storage;

using caps_info = ecsact::system_capabilities_info<SystemT>;
using adds_components = typename caps_info::adds_components;
Expand All @@ -84,7 +87,18 @@ void trivial_system_impl(
});

mp_for_each<removes_components>([&]<typename C>(C) {
info.template remove_component<C>(entity);
info.registry.template emplace<pending_remove<C>>(entity);

if constexpr(!C::transient) {
info.registry.template emplace<component_removed<C>>(entity);

auto& temp = info.registry.template storage<temp_storage<C>>();
if(temp.contains(entity)) {
temp.get(entity).value = info.registry.template get<C>(entity);
} else {
temp.emplace(entity, info.registry.template get<C>(entity));
}
}
});

each_callback(view, assoc_views, entity);
Expand Down
47 changes: 47 additions & 0 deletions runtime/test/runtime_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ void runtime_test::OtherEntitySystem::impl(context& ctx) {
void runtime_test::MakeAnother::impl(context& ctx) {
}

void runtime_test::TrivialRemove::impl(context& ctx) {
// This trivial remove should not even be required:
// SEE: https://github.com/ecsact-dev/ecsact_lang_cpp/issues/80
std::cerr << "TriviaLRemove impl called (SHOULD NOT HAPPEN)\n";
std::cerr.flush();
std::abort();
}

TEST(Core, CreateRegistry) {
auto reg_id = ecsact_create_registry("CreateRegistry");
EXPECT_NE(reg_id, ecsact_invalid_registry_id);
Expand Down Expand Up @@ -219,6 +227,45 @@ static void dynamic_impl(ecsact_system_execution_context* ctx) {
ecsact_system_execution_context_update(ctx, comp_id, &comp);
}

TEST(Core, TrivialRemoveEvent) {
auto reg_id = ecsact_create_registry("TrivialRemoveEvent");
auto entity = ecsact_create_entity(reg_id);

runtime_test::TrivialRemoveComponent comp{};
ecsact_add_component(
reg_id,
entity,
runtime_test::TrivialRemoveComponent::id,
&comp
);

ecsact_add_component(
reg_id,
entity,
runtime_test::WillRemoveTrivial::id,
nullptr
);

static bool event_happened = false;
ecsact_execution_events_collector ev_collector{};
ev_collector.remove_callback = //
[](
ecsact_event event,
ecsact_entity_id entity_id,
ecsact_component_id component_id,
const void* component_data,
void* callback_user_data
) {
event_happened = true;

EXPECT_EQ(component_id, runtime_test::TrivialRemoveComponent::id);
};

ecsact_execute_systems(reg_id, 1, nullptr, &ev_collector);

EXPECT_TRUE(event_happened);
}

TEST(Core, DynamicSystemImpl) {
ecsact::core::registry reg("DynamicSystemImpl");
auto entity = reg.create_entity();
Expand Down
7 changes: 7 additions & 0 deletions runtime/test/runtime_test.ecsact
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ system SimpleSystem {
readwrite ComponentA;
}

component WillRemoveTrivial;
component TrivialRemoveComponent { i32 example; }
system TrivialRemove {
include WillRemoveTrivial;
removes TrivialRemoveComponent;
}

system OtherEntitySystem {
readwrite OtherEntityComponent with target {
readwrite ComponentA;
Expand Down