LeeS
Imperator
You have a few errors in your code:
However, you still have a larger problem in that you have not assigned your code to run when the proper 'hook' event is triggered by the gamecore. So your code will never execute.
The way you programmed your code would seem like it would work from the lua hook event called PlayerDoTurn, but the problem is that doing so would cause all city populations to be decreased every turn once the tech is discovered, essentially resulting in an empire that would have 1-pop cities.
It would be better to use the hook GameEvents.TeamTechResearched(). The gamecore passes arguments iTeam, iTech, iChange to all functions subscribed to this GameEvent. Individual players, however, do not research or own technologies. Teams do. So it is necessary to use the data passed for 'iTeam' to find all players belonging to that team. The advantage to using GameEvents.TeamTechResearched is that it only can ever fire once for a given technology for a given team.
The code as written will not affect City-States or Barbarians.
- You have a mis-match between 'pCity' and 'city'.
- Since you defined your city variable as 'pCity' in this line
Code:
for pCity in pPlayer:Cities() do
Code:pCity:SetPopulation(pCity:GetPopulation()*0.67)
- You should probably also "floor" or "ceil" the result of the math. And you should probably also not adjust the city population unless the city population is above a minimum value.
- Since you defined your city variable as 'pCity' in this line
- You have never defined 'pTeam' so you would get an error for attempting to "index" a nil value
- You appear to be missing an 'end'
- 'function', 'for', and 'if' all require their own matching 'end'
Code:
local BigMistakePenalty = GameInfoTypes.FEATURE_MARSH
local iPrereq1 = GameInfo.Technologies["TECH_BIGMISTAKE"].ID
local iPrereq2 = GameInfo.Technologies["TECH_REMILITARIZATION"].ID
function BigMistakePenalty(iPlayer)
local pPlayer = Players[iPlayer]
if (Teams[pPlayer:GetTeam()]:IsHasTech(iPrereq2)) then
for pCity in pPlayer:Cities() do
local iCurrentPop = pCity:GetPopulation()
if (iCurrentPop >= 2) then
pCity:SetPopulation(math.floor(iCurrentPop * 0.67))
end
end
end
end
The way you programmed your code would seem like it would work from the lua hook event called PlayerDoTurn, but the problem is that doing so would cause all city populations to be decreased every turn once the tech is discovered, essentially resulting in an empire that would have 1-pop cities.
It would be better to use the hook GameEvents.TeamTechResearched(). The gamecore passes arguments iTeam, iTech, iChange to all functions subscribed to this GameEvent. Individual players, however, do not research or own technologies. Teams do. So it is necessary to use the data passed for 'iTeam' to find all players belonging to that team. The advantage to using GameEvents.TeamTechResearched is that it only can ever fire once for a given technology for a given team.
Code:
local iPrereq1 = GameInfo.Technologies["TECH_BIGMISTAKE"].ID
local iPrereq2 = GameInfo.Technologies["TECH_REMILITARIZATION"].ID
function BigMistakePenalty(iTeam, iTech, iChange)
if (iTech == iPrereq2) then
for iPlayer = 0, GameDefines.MAX_MAJOR_CIVS - 1, 1 do
if (Players[iPlayer] ~= nil) then
if (Players[iPlayer]:GetTeam() == iTeam) then
local pPlayer = Players[iPlayer]
for pCity in pPlayer:Cities() do
local iCurrentPop = pCity:GetPopulation()
if (iCurrentPop >= 2) then
pCity:SetPopulation(math.floor(iCurrentPop * 0.67))
end
end
end
end
end
end
end
GameEvents.TeamTechResearched.Add(BigMistakePenalty)