sman1975
Emperor
@LeeS - thanks for the suggestion about PlayerDoTurn. Have made the adjustments, even borrowed heavily from your UnitSpawnHandler mod (why this isn't used by every mod on the planet is a mystery to me...). Using this approach is much more efficient and seems to be more trustworthy during game play.
For the most part everything is set up well, but...
When the PlayerDoTurn function fires, it looks thru all a player's units, and if it finds an airfield, it looks at the TI for that plot. If it's not an airfield TI, it fires the "DismantleAirfield" function.
Dismantle Airfield basically loops through all units at that plot, and if their 'GetTransportUnit' is equal to the Airfield unit, it attempts to relocate them to a city with spare air capacity.
In testing, I have an airfield with 8 units that is being dismantled. Unfortunately, for some reason only half the units make the jump. The other half come back from the 'unit = plot:GetUnit(i)' statement as 'nil'
Not sure if it's relevant, but if I look at the list of units using IGE (mouse hovering over the plot) - on the list of 8 air units shown before the function executes - every other unit makes the jump (e.g. #1, 3, 5, and 7).
Here are some screenshots to hopefully better explain the before/after function executes:

I'm just confused as how any unit could come out of the 'local pPlotUnit = pAirfieldPlot:GetUnit( i )' statement as nil?
For the most part everything is set up well, but...

When the PlayerDoTurn function fires, it looks thru all a player's units, and if it finds an airfield, it looks at the TI for that plot. If it's not an airfield TI, it fires the "DismantleAirfield" function.
Dismantle Airfield basically loops through all units at that plot, and if their 'GetTransportUnit' is equal to the Airfield unit, it attempts to relocate them to a city with spare air capacity.
In testing, I have an airfield with 8 units that is being dismantled. Unfortunately, for some reason only half the units make the jump. The other half come back from the 'unit = plot:GetUnit(i)' statement as 'nil'
Not sure if it's relevant, but if I look at the list of units using IGE (mouse hovering over the plot) - on the list of 8 air units shown before the function executes - every other unit makes the jump (e.g. #1, 3, 5, and 7).
Here are some screenshots to hopefully better explain the before/after function executes:





I'm just confused as how any unit could come out of the 'local pPlotUnit = pAirfieldPlot:GetUnit( i )' statement as nil?
Spoiler Actual Code :
Code:
function DismantleAirfield(pPlayer, pUnit, pAirfieldPlot) -- Properly relocates aircraft, then deletes airfield unit
local iUnitCount = pAirfieldPlot:GetNumUnits() or 0 -- How many units (of any kind) are there?
local iRelocatedCount = 0
local iUnrelocatedCount = 0
local iAirfieldX = pUnit:GetX()
local iAirfieldY = pUnit:GetY()
if iUnitCount > 0 then -- If there are more than 0 units, then (should always be true, due to airfield unit)
dPrint("DismantleAirfield. Units to process: " .. iUnitCount)
for i = 0, iUnitCount - 1, 1 do -- Loop thru all units on this plot
local pPlotUnit = pAirfieldPlot:GetUnit( i ) -- Get the Unit pointer for unit 'i'
dPrint("================ i: " .. i)
if pPlotUnit then
dPrint("Unit: " .. pPlotUnit:GetName() .. " (" ..pPlotUnit:GetID() .. ")" )
if pPlotUnit:GetTransportUnit() == pUnit then -- If the pPlotUnit is cargo of pUnit, then... (unit belongs to the airfield unit)
dPrint("Unit was stationed at this " .. pUnit:GetName())
local pRelocationCity = FindEmptyCity(pPlayer) -- Find a city with spare air unit capacity
if pRelocationCity then -- If there is such a city, then
local iOldX = pPlotUnit:GetX() -- Get old X location for debug
local iOldY = pPlotUnit:GetY() -- Get old X location for debug
local x = pRelocationCity:GetX() -- Get new city X location for jumping
local y = pRelocationCity:GetY() -- Get new city Y location for jumping
pPlotUnit:SetXY(x,y) -- Physically relocate the unit to the new city's X/Y
iRelocatedCount = iRelocatedCount + 1
dPrint("Relocating Unit " .. pPlotUnit:GetName() .. " to " .. pRelocationCity:GetName() .. ". Old Location: " .. iOldX .. " / " .. iOldY .. ", New Location: " .. x .. " / " .. y)
else
iUnrelocatedCount = iUnrelocatedCount + 1
end -- Otherwise, skip this unit (which will ultimately be deleted)
end
else
print("PlotUnit nil")
end
end
end
local sHeader = pPlayer:GetCivilizationShortDescription() .. " dismantled a " .. pUnit:GetName()
local sMessage = pPlayer:GetCivilizationShortDescription() .. " has dismantled a " .. pUnit:GetName() .. ", located at Tile " .. pUnit:GetX() .. " / " .. pUnit:GetY() .. "."
if iRelocatedCount > 0 then
sMessage = sMessage .. " They managed to relocate " .. iRelocatedCount .. " aircraft."
end
if iUnrelocatedCount > 0 then
sMessage = sMessage .. " Unfortunately, " .. iUnrelocatedCount .. " aircraft could not be relocated and were lost."
end
AddAirfieldNotification(2, pPlayer, sHeader, sMessage, pUnit:GetUnitType(), iAirfieldX, iAirfieldY)
pUnit:Kill() -- Remove the Airfield unit from the map, along with all unrelocated planes
end
Last edited: