fix a bug with timer erasing

This commit is contained in:
red031000 2021-12-07 20:43:22 +00:00
parent 002f3b6c0e
commit 7bead89e39
No known key found for this signature in database
GPG Key ID: A3C87CFF2DC536C2

View File

@ -1222,13 +1222,19 @@ void Entity::UpdateXMLDoc(tinyxml2::XMLDocument* doc) {
}
void Entity::Update(const float deltaTime) {
for (int i = 0; i < m_Timers.size(); i++) {
int timerSize = m_Timers.size();
for (int i = 0; i < timerSize; i++) {
m_Timers[i]->Update(deltaTime);
if (m_Timers[i]->GetTime() <= 0) {
const auto timerName = m_Timers[i]->GetName();
delete m_Timers[i];
m_Timers.erase(m_Timers.begin() + i);
do { //sometimes, due to a race condition, m_Timers.erase doesn't actually erase, repeat until it does
if (m_Timers[i]->GetName() != timerName) {
break;
}
delete m_Timers[i];
m_Timers.erase(m_Timers.begin() + i);
} while (m_Timers.size() == timerSize); //timer size indicates whether it's actually successfully been erased or not
for (CppScripts::Script* script : CppScripts::GetEntityScripts(this)) {
script->OnTimerDone(this, timerName);