Karatekid5
Warlord
Building upon a previous mod I finished, I decided to add some code that removes the target player's policies and deletes their units (alongside the functions of removing all cities aside from the capital and removing all researched technologies from the target; for context this is a silly mod for AI-only games). While the policy removal loop works perfectly fine, looping through the PlotOwnerPlayer's units results in a C Stack Overflow that crashes the game. I defined the pPlotOwnerPlayer earlier in the code so it shouldn't be returning any nil values.
It's a simple unit loop, what is causing it to overflow? Is it caused by running this in the PreKill Routine?
Furthermore, is it possible to loop through and delete (via SetNumRealBuilding) a city's buildings? I saw it mentioned on another thread that you can't directly loop through them so I was wondering if there was another way.
Code:
include("FLuaVector.lua")
print("Big Chungus")
local iUnitBigChungus = GameInfoTypes.UNIT_BIG_CHUNGUS
function ChungusPrimalReversion(iPlayer,iUnit,_,iPlotX, iPlotY, bDelay)
local pPlayer = Players[iPlayer]
local pPlot = Map.GetPlot(iPlotX, iPlotY)
local pUnit = pPlayer:GetUnitByID(iUnit);
local iOriginalPlotOwner = pPlot:GetOwner()
if (iOriginalPlotOwner ~= -1) and pUnit:GetUnitType()==iUnitBigChungus then -- (iOriginalPlotOwner ~= iPlayer) then
local pPlotOwnerPlayer = Players[iOriginalPlotOwner]
for city in pPlotOwnerPlayer:Cities() do
if city:IsCapital() == false and city:IsOriginalMajorCapital() == false and city:IsHolyCityAnyReligion() == false then
local plot = city:Plot()
local hexpos = ToHexFromGrid(Vector2(plot:GetX(), plot:GetY()));
local cityID = city:GetID()
city:SetPopulation(0)
city:Kill()
print("Primal Reverted!")
Events.SerialEventCityDestroyed(hexpos, iOriginalPlotOwner, cityID, -1)
else
city:SetPopulation(1)
end
end
local RevertedTeam = Teams[pPlotOwnerPlayer:GetTeam()]
for tech in GameInfo.Technologies() do
if (tech.ID > 0) then
RevertedTeam:SetHasTech(tech.ID, false)
end
end
for policy in GameInfo.Policies() do
if (policy.ID > 0) then
pPlotOwnerPlayer:SetHasPolicy(policy.ID, false)
end
end
for pUnit in pPlotOwnerPlayer:Units() do
pUnit:Kill()
end
end
end
GameEvents.UnitPrekill.Add(ChungusPrimalReversion);
It's a simple unit loop, what is causing it to overflow? Is it caused by running this in the PreKill Routine?
Furthermore, is it possible to loop through and delete (via SetNumRealBuilding) a city's buildings? I saw it mentioned on another thread that you can't directly loop through them so I was wondering if there was another way.