From 531c4a594c1d549dc961364771ddd04ae9e791bb Mon Sep 17 00:00:00 2001 From: EmosewaMC <39972741+EmosewaMC@users.noreply.github.com> Date: Thu, 16 Jun 2022 17:50:33 -0700 Subject: [PATCH] remove children We need to make sure we are actually deleting children from the vector of children when they are deleted as entities. --- dGame/Entity.cpp | 14 ++++++++++++++ dGame/Entity.h | 1 + 2 files changed, 15 insertions(+) diff --git a/dGame/Entity.cpp b/dGame/Entity.cpp index 16b2f0af..e7a7bd17 100644 --- a/dGame/Entity.cpp +++ b/dGame/Entity.cpp @@ -125,6 +125,9 @@ Entity::~Entity() { m_Components.erase(pair.first); } + if (m_ParentEntity) { + m_ParentEntity->RemoveChild(this); + } } void Entity::Initialize() @@ -1656,6 +1659,17 @@ void Entity::AddChild(Entity* child) { m_ChildEntities.push_back(child); } +void Entity::RemoveChild(Entity* child) { + if (!child) return; + for (auto entity = m_ChildEntities.begin(); entity != m_ChildEntities.end(); entity++) { + if (*entity && (*entity)->GetObjectID() == child->GetObjectID()) { + m_IsParentChildDirty = true; + m_ChildEntities.erase(entity); + return; + } + } +} + void Entity::AddTimer(std::string name, float time) { EntityTimer* timer = new EntityTimer(name, time); m_Timers.push_back(timer); diff --git a/dGame/Entity.h b/dGame/Entity.h index f2bc4237..c804deaa 100644 --- a/dGame/Entity.h +++ b/dGame/Entity.h @@ -143,6 +143,7 @@ public: void SetProximityRadius(dpEntity* entity, std::string name); void AddChild(Entity* child); + void RemoveChild(Entity* child); void AddTimer(std::string name, float time); void AddCallbackTimer(float time, std::function callback); bool HasTimer(const std::string& name);