From b83a9b60e7cd69358fd0c053d56651616780c924 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Wed, 22 Nov 2017 10:26:33 +0100 Subject: [PATCH] Sdk/World: Fix destruction retrieval while world clear --- ChangeLog.md | 1 + SDK/include/NDK/World.inl | 2 +- SDK/src/NDK/Entity.cpp | 2 +- SDK/src/NDK/World.cpp | 11 ++++++++--- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 9811705dd..f252271fe 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -34,6 +34,7 @@ Nazara Development Kit: - TextAreaWidget will now show a cursor as long as it has focus - Fix BaseWidget linking error on Linux - ⚠️ Rewrite StateMachine to fix instantaneous state changing (state change is no longer effective until the next update call) +- Fix entities destruction when coming from World::Clear() (also called by destructor), which invalidated world entities handles before destroying entities (preventing destruction callback to get valid entities handles from world) # 0.4: diff --git a/SDK/include/NDK/World.inl b/SDK/include/NDK/World.inl index c5ea48db1..88e33ec1d 100644 --- a/SDK/include/NDK/World.inl +++ b/SDK/include/NDK/World.inl @@ -197,7 +197,7 @@ namespace Ndk * * \param id Identifier of the entity * - * \remark Handle referenced by this function can move in memory when updating the world, do not keep a reference to a handle from a world update to another + * \remark Handle referenced by this function can move in memory when updating the world, do not keep a handle reference from a world update to another * \remark If an invalid identifier is provided, an error got triggered and an invalid handle is returned */ inline const EntityHandle& World::GetEntity(EntityId id) diff --git a/SDK/src/NDK/Entity.cpp b/SDK/src/NDK/Entity.cpp index fab08b237..a417a4414 100644 --- a/SDK/src/NDK/Entity.cpp +++ b/SDK/src/NDK/Entity.cpp @@ -56,7 +56,7 @@ namespace Ndk Entity::~Entity() { - if (m_world) + if (m_world && m_valid) Destroy(); } diff --git a/SDK/src/NDK/World.cpp b/SDK/src/NDK/World.cpp index 702452fc4..41336c71b 100644 --- a/SDK/src/NDK/World.cpp +++ b/SDK/src/NDK/World.cpp @@ -115,10 +115,15 @@ namespace Ndk void World::Clear() noexcept { - // First, destruction of entities, then handles - // This is made to avoid that handle warn uselessly entities before their destruction - m_entities.clear(); + // Destroy every valid entity first, to ensure entities are still accessible by ID while being destroyed + for (EntityBlock* entBlock : m_entityBlocks) + { + if (entBlock->entity.IsValid()) + entBlock->entity.Destroy(); + } m_entityBlocks.clear(); + + m_entities.clear(); m_freeIdList.clear(); m_waitingEntities.clear();