Skip to content

Commit

Permalink
fix: update events happening too often (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
zaucy committed May 22, 2024
1 parent 0f71d39 commit fa98277
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 10 deletions.
1 change: 1 addition & 0 deletions ecsact/entt/detail/apply_pending.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ auto apply_pending_add(::entt::registry& registry) -> void {
registry.view<pending_add<C>>().each(
[&](auto entity, const pending_add<C>& comp) {
registry.emplace<C>(entity, comp.value);
registry.emplace<beforechange_storage<C>>(entity, comp.value, false);
}
);
}
Expand Down
3 changes: 2 additions & 1 deletion ecsact/entt/detail/internal_markers.hh
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ struct beforeremove_storage<C> {
template<typename C>
requires(!std::is_empty_v<C>)
struct beforechange_storage {
C value;
C value;
bool has_update_occurred = false;
};

template<typename C>
Expand Down
4 changes: 3 additions & 1 deletion ecsact/entt/wrapper/core.hh
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ inline auto add_component( //
} else {
reg.emplace<detail::beforechange_storage<C>>(
entity,
*static_cast<const C*>(component_data)
*static_cast<const C*>(component_data),
false
);
reg.emplace<C>(entity, *static_cast<const C*>(component_data));
}
Expand Down Expand Up @@ -310,6 +311,7 @@ auto _trigger_update_component_event(
for(ecsact::entt::entity_id entity : changed_view) {
auto& before = changed_view.template get<beforechange_storage<C>>(entity);
auto& current = changed_view.template get<C>(entity);
before.has_update_occurred = false;

if(before.value != current) {
events_collector.invoke_update_callback<C>(entity, current);
Expand Down
13 changes: 7 additions & 6 deletions ecsact/entt/wrapper/dynamic.hh
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ auto context_add(
const C* component = static_cast<const C*>(component_data);
registry.template emplace_or_replace<pending_add<C>>(entity, *component);

auto& before_change =
registry.template emplace_or_replace<beforechange_storage<C>>(entity);

before_change.value = *component;

registry.template remove<beforeremove_storage<C>>(entity);
}

Expand Down Expand Up @@ -151,6 +146,7 @@ auto context_update(
const void* in_component_data
) -> void {
using ecsact::entt::component_updated;
using ecsact::entt::detail::beforechange_storage;
// TODO(Kelwan): for remove, beforeremove_storage

auto entity = context->entity;
Expand All @@ -159,8 +155,13 @@ auto context_update(
const auto& in_component = *static_cast<const C*>(in_component_data);

auto& current_component = registry.template get<C>(entity);
auto& beforechange = registry.template get<beforechange_storage<C>>(entity);
if(!beforechange.has_update_occurred) {
beforechange.value = current_component;
beforechange.has_update_occurred = true;
registry.template emplace_or_replace<component_updated<C>>(entity);
}
current_component = in_component;
registry.template emplace_or_replace<component_updated<C>>(entity);
}

template<typename C>
Expand Down
5 changes: 5 additions & 0 deletions test/imported_pkg.ecsact
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ component SomeLocalComponent {
i32 local_num;
}

component Incrementer {
i32 increment_value;
}

system ImportedSystem {
readwrite SomeLocalComponent;
readonly Incrementer;
}

25 changes: 23 additions & 2 deletions test/runtime_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,15 @@ void runtime_test::TrivialRemove::impl(context& ctx) {

void runtime_test::SimpleIncrementImportedComp::impl(context& ctx) {
auto comp = ctx.get<imported::test_pkg::ImportedComponent>();
comp.num += 1;
auto incrementer = ctx.get<imported::test_pkg::Incrementer>();
comp.num += incrementer.increment_value;
ctx.update(comp);
}

void imported::test_pkg::ImportedSystem::impl(context& ctx) {
auto comp = ctx.get<SomeLocalComponent>();
comp.local_num += 1;
auto incrementer = ctx.get<Incrementer>();
comp.local_num += incrementer.increment_value;
ctx.update(comp);
}

Expand Down Expand Up @@ -864,6 +866,7 @@ TEST(Core, CreateAndDestroyEntity) {

TEST(Core, MultiPkgUpdate) {
using imported::test_pkg::ImportedComponent;
using imported::test_pkg::Incrementer;
using imported::test_pkg::SomeLocalComponent;

ASSERT_TRUE(ecsact_set_system_execution_impl(
Expand All @@ -883,8 +886,10 @@ TEST(Core, MultiPkgUpdate) {
auto test_entity = reg.create_entity();
reg.add_component(test_entity, ImportedComponent{});
reg.add_component(test_entity, SomeLocalComponent{});
reg.add_component(test_entity, Incrementer{});

for(int i = 0; 10 > i; ++i) {
reg.update_component(test_entity, Incrementer{1});
auto event_happened = std::set<std::type_index>{};
auto evc = ecsact::core::execution_events_collector<>{};
evc.set_update_callback<ImportedComponent>([&](auto entity, auto comp) {
Expand All @@ -908,6 +913,22 @@ TEST(Core, MultiPkgUpdate) {
EXPECT_EQ(c.local_num, i + 1);
EXPECT_TRUE(event_happened.contains(typeid(SomeLocalComponent)));
}

event_happened.clear();

reg.update_component(test_entity, Incrementer{0});
reg.execute_systems(1, evc);
{
auto c = reg.get_component<ImportedComponent>(test_entity);
EXPECT_EQ(c.num, i + 1);
EXPECT_FALSE(event_happened.contains(typeid(ImportedComponent)));
}

{
auto c = reg.get_component<SomeLocalComponent>(test_entity);
EXPECT_EQ(c.local_num, i + 1);
EXPECT_FALSE(event_happened.contains(typeid(SomeLocalComponent)));
}
}
}

Expand Down
1 change: 1 addition & 0 deletions test/runtime_test.ecsact
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ component EntityTesting {

system SimpleIncrementImportedComp {
readwrite imported.test_pkg.ImportedComponent;
readonly imported.test_pkg.Incrementer;
}

system AddsAutoRemovedTag {
Expand Down

0 comments on commit fa98277

Please # to comment.