Skip to content

Commit 812e693

Browse files
committed
Fix remaining 7 code review issues
1-3. Add null checks for type and weaponDef before accessing in perferedUnit 4. Fix Spring.GetUnitCommands parameter - use fixed 100 instead of variable stockpile 5. Add null check for unit position before using in distance calculation 6. Add null check for tabButton.button before calling SetVisibility 7. Initialize WG.missileTotalCount in widget:Initialize to fix badge when disabled All fixes are defensive nil/validation checks to prevent crashes. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GiaPfKSj8kGFjaUPyxYYHd
1 parent 7173b99 commit 812e693

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

LuaUI/Widgets/gui_chili_integral_menu.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2604,7 +2604,7 @@ function widget:Update()
26042604
end
26052605

26062606
-- Update tab visibility for panels with dynamic visibility
2607-
if panelData.name == "missiles" and panelData.tabButton then
2607+
if panelData.name == "missiles" and panelData.tabButton and panelData.tabButton.button then
26082608
local hasCommands = false
26092609
local customCommands = widgetHandler.customCommands
26102610
for j = 1, #customCommands do

LuaUI/Widgets/missle_command_center.lua

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ function widget:GetInfo()
1414
}
1515
end
1616

17+
function widget:Initialize()
18+
WG.missileTotalCount = 0
19+
end
20+
1721
--------------------------------------------------------------------------------
1822
--------------------------------------------------------------------------------
1923

@@ -81,7 +85,7 @@ local function missle_class()
8185
local numStockpiled = unitType.getStockpile(unit)
8286
if not numStockpiled or numStockpiled == 0 then return 0 end
8387

84-
local cmdQueue = Spring.GetUnitCommands(unit, numStockpiled)
88+
local cmdQueue = Spring.GetUnitCommands(unit, 100)
8589
if not cmdQueue then return 0 end
8690

8791
local numQueued = 0
@@ -142,10 +146,14 @@ local function missle_class()
142146

143147
function self:perferedUnit(unit1, unit2, params)
144148
local unit2x, _, unit2z = Spring.GetUnitPosition(unit2)
145-
local unit2Dist = distance(params.x, params.z, unit2x, unit2z)
149+
if not unit2x then return unit1 end
146150

147151
local type2 = self.launchableTypes[Spring.GetUnitDefID(unit2)]
152+
if not type2 then return unit1 end
153+
154+
local unit2Dist = distance(params.x, params.z, unit2x, unit2z)
148155
local weaponDef2 = WeaponDefs[UnitDefs[Spring.GetUnitDefID(unit2)].weapons[type2.weaponId].weaponDef]
156+
if not weaponDef2 then return unit1 end
149157

150158
local range = weaponDef2.range
151159

@@ -154,7 +162,10 @@ local function missle_class()
154162
if not unit1 then return unit2 end
155163

156164
local type1 = self.launchableTypes[Spring.GetUnitDefID(unit1)]
165+
if not type1 then return unit2 end
166+
157167
local weaponDef1 = WeaponDefs[UnitDefs[Spring.GetUnitDefID(unit1)].weapons[type1.weaponId].weaponDef]
168+
if not weaponDef1 then return unit2 end
158169

159170
local unit1Silo = Spring.GetUnitRulesParam(unit1, "missile_parentSilo")
160171
local unit1Selected = params.selectedUnits[unit1] or (unit1Silo and params.selectedUnits[unit1Silo])
@@ -244,6 +255,7 @@ local function missle_class()
244255
local unit = self:getPerferedUnit{x = x, z = z}
245256
if not unit then return true end
246257
local unitType = self.launchableTypes[Spring.GetUnitDefID(unit)]
258+
if not unitType then return true end
247259

248260
Spring.GiveOrderToUnit(unit, CMD.INSERT, {0, unitType.launchCmd, CMD.OPT_SHIFT, unpack(cmdParams)}, CMD.OPT_ALT)
249261
return true

0 commit comments

Comments
 (0)