remove children

We need to make sure we are actually deleting children from the vector of children when they are deleted as entities.
This commit is contained in:
EmosewaMC 2022-06-16 17:50:33 -07:00
parent 4a39221dd0
commit 531c4a594c
2 changed files with 15 additions and 0 deletions

View File

@ -125,6 +125,9 @@ Entity::~Entity() {
m_Components.erase(pair.first); m_Components.erase(pair.first);
} }
if (m_ParentEntity) {
m_ParentEntity->RemoveChild(this);
}
} }
void Entity::Initialize() void Entity::Initialize()
@ -1656,6 +1659,17 @@ void Entity::AddChild(Entity* child) {
m_ChildEntities.push_back(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) { void Entity::AddTimer(std::string name, float time) {
EntityTimer* timer = new EntityTimer(name, time); EntityTimer* timer = new EntityTimer(name, time);
m_Timers.push_back(timer); m_Timers.push_back(timer);

View File

@ -143,6 +143,7 @@ public:
void SetProximityRadius(dpEntity* entity, std::string name); void SetProximityRadius(dpEntity* entity, std::string name);
void AddChild(Entity* child); void AddChild(Entity* child);
void RemoveChild(Entity* child);
void AddTimer(std::string name, float time); void AddTimer(std::string name, float time);
void AddCallbackTimer(float time, std::function<void()> callback); void AddCallbackTimer(float time, std::function<void()> callback);
bool HasTimer(const std::string& name); bool HasTimer(const std::string& name);