Skip to content

Commit 95ade0a

Browse files
authored
fixes trivial removes systems not triggering an event (#25)
1 parent b1a3c4b commit 95ade0a

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

ecsact/entt/trivial_system_impl.hh

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ void trivial_system_impl(
6767
EachCallbackT&& each_callback = [](auto&, auto&, auto) {}
6868
) {
6969
using boost::mp11::mp_for_each;
70+
using ecsact::entt::component_removed;
71+
using ecsact::entt::detail::pending_remove;
72+
using ecsact::entt::detail::temp_storage;
7073

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

8689
mp_for_each<removes_components>([&]<typename C>(C) {
87-
info.template remove_component<C>(entity);
90+
info.registry.template emplace<pending_remove<C>>(entity);
91+
92+
if constexpr(!C::transient) {
93+
info.registry.template emplace<component_removed<C>>(entity);
94+
95+
auto& temp = info.registry.template storage<temp_storage<C>>();
96+
if(temp.contains(entity)) {
97+
temp.get(entity).value = info.registry.template get<C>(entity);
98+
} else {
99+
temp.emplace(entity, info.registry.template get<C>(entity));
100+
}
101+
}
88102
});
89103

90104
each_callback(view, assoc_views, entity);

runtime/test/runtime_test.cc

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ void runtime_test::OtherEntitySystem::impl(context& ctx) {
2929
void runtime_test::MakeAnother::impl(context& ctx) {
3030
}
3131

32+
void runtime_test::TrivialRemove::impl(context& ctx) {
33+
// This trivial remove should not even be required:
34+
// SEE: https://github.com/ecsact-dev/ecsact_lang_cpp/issues/80
35+
std::cerr << "TriviaLRemove impl called (SHOULD NOT HAPPEN)\n";
36+
std::cerr.flush();
37+
std::abort();
38+
}
39+
3240
TEST(Core, CreateRegistry) {
3341
auto reg_id = ecsact_create_registry("CreateRegistry");
3442
EXPECT_NE(reg_id, ecsact_invalid_registry_id);
@@ -219,6 +227,45 @@ static void dynamic_impl(ecsact_system_execution_context* ctx) {
219227
ecsact_system_execution_context_update(ctx, comp_id, &comp);
220228
}
221229

230+
TEST(Core, TrivialRemoveEvent) {
231+
auto reg_id = ecsact_create_registry("TrivialRemoveEvent");
232+
auto entity = ecsact_create_entity(reg_id);
233+
234+
runtime_test::TrivialRemoveComponent comp{};
235+
ecsact_add_component(
236+
reg_id,
237+
entity,
238+
runtime_test::TrivialRemoveComponent::id,
239+
&comp
240+
);
241+
242+
ecsact_add_component(
243+
reg_id,
244+
entity,
245+
runtime_test::WillRemoveTrivial::id,
246+
nullptr
247+
);
248+
249+
static bool event_happened = false;
250+
ecsact_execution_events_collector ev_collector{};
251+
ev_collector.remove_callback = //
252+
[](
253+
ecsact_event event,
254+
ecsact_entity_id entity_id,
255+
ecsact_component_id component_id,
256+
const void* component_data,
257+
void* callback_user_data
258+
) {
259+
event_happened = true;
260+
261+
EXPECT_EQ(component_id, runtime_test::TrivialRemoveComponent::id);
262+
};
263+
264+
ecsact_execute_systems(reg_id, 1, nullptr, &ev_collector);
265+
266+
EXPECT_TRUE(event_happened);
267+
}
268+
222269
TEST(Core, DynamicSystemImpl) {
223270
ecsact::core::registry reg("DynamicSystemImpl");
224271
auto entity = reg.create_entity();

runtime/test/runtime_test.ecsact

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ system SimpleSystem {
1515
readwrite ComponentA;
1616
}
1717

18+
component WillRemoveTrivial;
19+
component TrivialRemoveComponent { i32 example; }
20+
system TrivialRemove {
21+
include WillRemoveTrivial;
22+
removes TrivialRemoveComponent;
23+
}
24+
1825
system OtherEntitySystem {
1926
readwrite OtherEntityComponent with target {
2027
readwrite ComponentA;

0 commit comments

Comments
 (0)