Skip to content

Commit a38a1e1

Browse files
committed
fix crashes caused by interacting with associated components
1 parent a953f5b commit a38a1e1

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

ecsact/entt/runtime.hh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,11 @@ private:
677677

678678
auto& assoc_view = std::get<I>(assoc_views);
679679
auto& assoc_view_itr = std::get<I>(assoc_views_itrs);
680+
if(assoc_view.begin() == assoc_view.end()) {
681+
missing_assoc_entities = true;
682+
return;
683+
}
684+
680685
assert(view.contains(entity));
681686
auto& comp = view.template get<ComponentT>(entity);
682687

runtime/test/runtime_test.cc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ void runtime_test::AttackDamageWeakened::impl(context& ctx) {
7373
// target_ctx.update(target_health);
7474
}
7575

76+
void runtime_test::AddAssocTest::impl(context& ctx) {
77+
auto other_entity = ctx.get<OtherEntityComponent>();
78+
79+
// Get Target other context from OtherEntityComponent
80+
auto target_ctx = ctx._ctx.other(other_entity.target);
81+
target_ctx.add(AddAssocTestComponent{.num = 10});
82+
}
83+
7684
TEST(Core, CreateRegistry) {
7785
auto reg_id = ecsact_create_registry("CreateRegistry");
7886
EXPECT_NE(reg_id, ecsact_invalid_registry_id);
@@ -476,6 +484,40 @@ TEST(Core, ExecuteSystemsAssocActionOk) {
476484
EXPECT_EQ(exec_err, ECSACT_EXEC_SYS_OK);
477485
}
478486

487+
TEST(Core, AddAssocOk) {
488+
ecsact_set_system_execution_impl(
489+
ecsact_id_cast<ecsact_system_like_id>(runtime_test::AddAssocTest::id),
490+
&runtime_test__AddAssocTest
491+
);
492+
493+
auto reg = ecsact::core::registry("AddAssocOk");
494+
auto test_entity1 = reg.create_entity();
495+
reg.add_component(
496+
test_entity1,
497+
runtime_test::ComponentA{
498+
.a = 42,
499+
}
500+
);
501+
502+
auto test_entity2 = reg.create_entity();
503+
reg.add_component<runtime_test::AddAssocTestTag>(test_entity2);
504+
505+
auto options = ecsact_execution_options{};
506+
auto test_action = runtime_test::AssocTestAction{
507+
.assoc_entity = test_entity2,
508+
};
509+
auto test_action_c = ecsact_action{
510+
.action_id = runtime_test::AssocTestAction::id,
511+
.action_data = &test_action,
512+
};
513+
514+
options.actions_length = 1;
515+
options.actions = &test_action_c;
516+
auto exec_err = ecsact_execute_systems(reg.id(), 1, &options, nullptr);
517+
518+
EXPECT_EQ(exec_err, ECSACT_EXEC_SYS_OK);
519+
}
520+
479521
TEST(Core, AssociationEntityCorrectness) {
480522
using runtime_test::AttackDamage;
481523
using runtime_test::AttackDamageWeakened;

runtime/test/runtime_test.ecsact

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ action AssocTestAction {
6161
adds OtherEntityComponent;
6262
}
6363

64+
component AddAssocTestComponent {
65+
i32 num;
66+
}
67+
68+
component AddAssocTestTag;
69+
70+
system AddAssocTest {
71+
readwrite OtherEntityComponent with target {
72+
include AddAssocTestTag;
73+
adds AddAssocTestComponent;
74+
}
75+
}
76+
6477
system AttackDamage {
6578
readwrite Attacking with target {
6679
readwrite Health;

0 commit comments

Comments
 (0)