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

1418 destroy child entities when parent entities are destroyed #1465

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- Entity destruction detection to observers (#1458, **@kuukitenshi**).

- Destroy child entities when parent entities are destroyed (#1418, **@kuukitenshi**).

## [v0.6.0] - 2025-02-10

Expand Down
4 changes: 2 additions & 2 deletions core/src/ecs/command_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ Entity CommandBuffer::create()
void CommandBuffer::destroy(Entity entity)
{
std::lock_guard<std::mutex> lock(mMutex);

mCommands.emplace_back([entity](World& world) { world.destroy(entity); });
mCommands.emplace_back([entity](World& world) { world.destroy(entity);});
}

std::unordered_map<std::string, Entity> CommandBuffer::spawn(const Blueprint& blueprint, bool withName)
Expand Down
12 changes: 12 additions & 0 deletions engine/src/transform/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ void cubos::engine::transformPlugin(Cubos& cubos)
.without<LocalToParent>()
.call(addComponent<LocalToParent>);

cubos.observer("destroy childs when parent is destroyed")
.onDestroy(0)
.call([](Query<Entity> query, Query<Entity, const ChildOf&> children, Commands cmds) {
for (auto [entity] : query)
{
for (auto [child, childOf] : children.pin(1, entity))
{
cmds.destroy(child);
}
}
});

cubos.system("update LocalToParent's")
.tagged(transformUpdateTag)
.before(transformUpdatePropagateTag)
Expand Down
Loading