Skip to content
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

fix: two small fixes to crash causing bugs #1347

Closed
wants to merge 3 commits into from
Closed
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
9 changes: 8 additions & 1 deletion core/src/ecs/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -806,13 +806,20 @@ auto World::Components::remove(const reflection::Type& type) -> Components&
return *this;
}

// Trigger any observers that are interested in this change.
// Trigger any observers that are interested in this change, before component is removed.
CommandBuffer cmdBuffer{mWorld};
if (mWorld.mObservers->notifyRemove(cmdBuffer, mEntity, columnId))
{
cmdBuffer.commit();
}

// Update old archetype since observers might have modified it.
oldArchetype = mWorld.mEntityPool.archetype(mEntity.index);
if (!mWorld.mArchetypeGraph.contains(oldArchetype, columnId))
{
return *this;
}

auto& oldTable = mWorld.mTables.dense().at(oldArchetype);

// Otherwise, we'll need to move the entity to a new archetype.
Expand Down
34 changes: 34 additions & 0 deletions core/tests/ecs/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <doctest/doctest.h>

#include <cubos/core/ecs/observer/observers.hpp>
#include <cubos/core/ecs/world.hpp>
#include <cubos/core/reflection/external/primitives.hpp>
#include <cubos/core/reflection/type.hpp>
Expand Down Expand Up @@ -547,5 +548,38 @@ TEST_CASE("ecs::World")
CHECK(it->type->is<IntegerRelation>());
}
}

SUBCASE("world remove works correctly with remove observers")
{
auto foo = world.create();
auto bar = world.create();

world.components(foo).add(IntegerComponent{0});
auto columnId = cubos::core::ecs::ColumnId::make(world.types().id<IntegerComponent>());
world.observers().hookOnRemove(columnId, cubos::core::ecs::System<void>::make(world, [foo, bar](World& w) {
w.components(foo).add(ParentComponent{bar});
}));
// Removing IntegerComponent triggers observer, which alters the entity
world.components(foo).remove<IntegerComponent>();
CHECK_FALSE(world.components(foo).has<IntegerComponent>());
CHECK(world.components(foo).has<ParentComponent>());
}

SUBCASE("world destroy works correctly with remove observers")
{
auto foo = world.create();
auto bar = world.create();

world.components(foo).add(IntegerComponent{0});
auto columnId = cubos::core::ecs::ColumnId::make(world.types().id<IntegerComponent>());
world.observers().hookOnRemove(columnId, cubos::core::ecs::System<void>::make(world, [foo, bar](World& w) {
w.components(foo).add(ParentComponent{bar});
w.components(bar).add(ParentComponent{foo});
}));
// Destroying foo triggers observer, which alters the entity
world.destroy(foo);
CHECK_FALSE(world.isAlive(foo));
CHECK(world.components(bar).has<ParentComponent>());
}
}
// NOLINTEND(readability-function-size)
3 changes: 1 addition & 2 deletions engine/src/tools/play_pause/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ void cubos::engine::playPauseToolPlugin(Cubos& cubos)
{
state.scale = 1.0F;
}

ImGui::End();
}
ImGui::End();

if (!state.paused)
{
Expand Down
Loading