From 0a4ef2b3ae7018ae91d08158ca0dcd63ed109dc9 Mon Sep 17 00:00:00 2001 From: Saurtron Date: Thu, 5 Dec 2024 20:51:18 +0100 Subject: [PATCH 1/2] Protect second if from unit having been destroyed and thus its element at the table invalidated. --- luarules/gadgets/unit_evolution.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/luarules/gadgets/unit_evolution.lua b/luarules/gadgets/unit_evolution.lua index 479d8491cfd..73175c0d63f 100644 --- a/luarules/gadgets/unit_evolution.lua +++ b/luarules/gadgets/unit_evolution.lua @@ -349,7 +349,8 @@ if gadgetHandler:IsSyncedCode() then Evolve(unitID, evolutionMetaList[unitID].evolution_target) end end - if evolutionMetaList[unitID].evolution_condition == "timer_global" and currentTime >= evolutionMetaList[unitID].evolution_timer then + -- evolutionMetaList[unitID] might have been invalidated if Evolve() was called before this, since that will destroy the unit. + if evolutionMetaList[unitID] and evolutionMetaList[unitID].evolution_condition == "timer_global" and currentTime >= evolutionMetaList[unitID].evolution_timer then local enemyNearby = spGetUnitNearestEnemy(unitID, evolutionMetaList[unitID].combatRadius) local inCombat = false if enemyNearby then From 92965821d08b868291852eed254d6f0426c52d75 Mon Sep 17 00:00:00 2001 From: Saurtron Date: Fri, 6 Dec 2024 13:54:35 +0100 Subject: [PATCH 2/2] Merge both evolution condition checks into one. --- luarules/gadgets/unit_evolution.lua | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/luarules/gadgets/unit_evolution.lua b/luarules/gadgets/unit_evolution.lua index 73175c0d63f..ff4d1a798e3 100644 --- a/luarules/gadgets/unit_evolution.lua +++ b/luarules/gadgets/unit_evolution.lua @@ -337,20 +337,8 @@ if gadgetHandler:IsSyncedCode() then lastTimerCheck = f for unitID, _ in pairs(evolutionMetaList) do local currentTime = spGetGameSeconds() - if evolutionMetaList[unitID].evolution_condition == "timer" and (currentTime-evolutionMetaList[unitID].timeCreated) >= evolutionMetaList[unitID].evolution_timer then - local enemyNearby = spGetUnitNearestEnemy(unitID, evolutionMetaList[unitID].combatRadius) - local inCombat = false - if enemyNearby then - inCombat = true - evolutionMetaList[unitID].combatTimer = spGetGameSeconds() - end - - if not inCombat and (currentTime-evolutionMetaList[unitID].combatTimer) >= 5 then - Evolve(unitID, evolutionMetaList[unitID].evolution_target) - end - end - -- evolutionMetaList[unitID] might have been invalidated if Evolve() was called before this, since that will destroy the unit. - if evolutionMetaList[unitID] and evolutionMetaList[unitID].evolution_condition == "timer_global" and currentTime >= evolutionMetaList[unitID].evolution_timer then + if (evolutionMetaList[unitID].evolution_condition == "timer" and (currentTime-evolutionMetaList[unitID].timeCreated) >= evolutionMetaList[unitID].evolution_timer) or + (evolutionMetaList[unitID].evolution_condition == "timer_global" and currentTime >= evolutionMetaList[unitID].evolution_timer) then local enemyNearby = spGetUnitNearestEnemy(unitID, evolutionMetaList[unitID].combatRadius) local inCombat = false if enemyNearby then