Address timers being iterated through poorly (#646)

* Fix timers

* Update Entity.cpp

* Fix timers

Fix timers

Remove debug logs

remove _dynamic

* I like to move it move it
This commit is contained in:
David Markowitz 2022-07-16 18:21:35 -07:00 committed by GitHub
parent df0f11c95b
commit f5ae5aa13e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 7 deletions

View File

@ -1201,17 +1201,21 @@ void Entity::UpdateXMLDoc(tinyxml2::XMLDocument* doc) {
} }
void Entity::Update(const float deltaTime) { void Entity::Update(const float deltaTime) {
for (int i = 0; i < m_Timers.size(); i++) { uint32_t timerPosition;
m_Timers[i]->Update(deltaTime); timerPosition = 0;
if (m_Timers[i]->GetTime() <= 0) { while (timerPosition < m_Timers.size()) {
const auto timerName = m_Timers[i]->GetName(); m_Timers[timerPosition]->Update(deltaTime);
if (m_Timers[timerPosition]->GetTime() <= 0) {
const auto timerName = m_Timers[timerPosition]->GetName();
delete m_Timers[i]; delete m_Timers[timerPosition];
m_Timers.erase(m_Timers.begin() + i); m_Timers.erase(m_Timers.begin() + timerPosition);
for (CppScripts::Script* script : CppScripts::GetEntityScripts(this)) { for (CppScripts::Script* script : CppScripts::GetEntityScripts(this)) {
script->OnTimerDone(this, timerName); script->OnTimerDone(this, timerName);
} }
} else {
timerPosition++;
} }
} }
@ -1223,6 +1227,14 @@ void Entity::Update(const float deltaTime) {
m_CallbackTimers.erase(m_CallbackTimers.begin() + i); m_CallbackTimers.erase(m_CallbackTimers.begin() + i);
} }
} }
// Add pending timers to the list of timers so they start next tick.
if (m_PendingTimers.size() > 0) {
for (auto namedTimer : m_PendingTimers) {
m_Timers.push_back(namedTimer);
}
m_PendingTimers.clear();
}
if (IsSleeping()) if (IsSleeping())
{ {
@ -1661,7 +1673,7 @@ void Entity::RemoveChild(Entity* child) {
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_PendingTimers.push_back(timer);
} }
void Entity::AddCallbackTimer(float time, std::function<void()> callback) { void Entity::AddCallbackTimer(float time, std::function<void()> callback) {

View File

@ -309,6 +309,7 @@ protected:
std::unordered_map<int32_t, Component*> m_Components; //The int is the ID of the component std::unordered_map<int32_t, Component*> m_Components; //The int is the ID of the component
std::vector<EntityTimer*> m_Timers; std::vector<EntityTimer*> m_Timers;
std::vector<EntityTimer*> m_PendingTimers;
std::vector<EntityCallbackTimer*> m_CallbackTimers; std::vector<EntityCallbackTimer*> m_CallbackTimers;
bool m_ShouldDestroyAfterUpdate = false; bool m_ShouldDestroyAfterUpdate = false;