Bobert13
Prince
- Joined
- Feb 25, 2013
- Messages
- 346
I saw this method mentioned in a number of threads but I couldn't find a concrete example of it anywhere.
It's best to include a third check for some conditional in the line where we ensure the player isn't nil (city-state/barbarian) and is alive. Where I've placed ###Your code here### is where you'd either assign your bonus (unit, yield, dummy policy/building etc.) or you could call a function.
This method is robust and takes effect immediately upon entering the Era unlike SerialEventEraChanged. It's also triggered when bulbing techs through Scientests and should a mod add techs that are out of sequence we're verifying those too.
Code:
function OnResearched(iTeamID, eTech, iChange)
local pPlayer
--this loop assigns the pPlayer object
for iPlayer = 0, GameDefines.MAX_MAJOR_CIVS-1, 1 do
pPlayer = Players[iPlayer]
if (pPlayer:GetTeam() == iTeamID) then
break
end
end
if pPlayer ~= nil and pPlayer:IsAlive() then
--print(string.format("Some Player meeting the conditions has researched a new TECH!! =P"))
local playerEra = pPlayer:GetCurrentEra()
local techEra = GameInfoTypes[GameInfo.Technologies[eTech].Era]
if techEra > playerEra then
--print(string.format("Tech Era > Player Era. Giving out bonus points."))
###Your code here###
elseif techEra == playerEra then
--print(string.format("Tech Era = Player Era. Checking to see if it's the last tech to be researched for that Era."))
local pTeam = Teams[iTeamID]
local newEra = true
for row in GameInfo.Technologies() do
local techEra = GameInfoTypes[row.Era]
--print(string.format("playerEra = %i, techEra = %i", playerEra, techEra))
if techEra == playerEra then
if (pTeam:IsHasTech(row.ID) == false) then
--print(string.format("Bailing because the player hasn't finished all of the techs for the Era."))
newEra = false
break
end
end
end
if newEra then
--print(string.format("New Era Detected based on finishing all techs of an era."))
###Your code here###
end
end
end
end
GameEvents.TeamTechResearched.Add(OnResearched)
It's best to include a third check for some conditional in the line where we ensure the player isn't nil (city-state/barbarian) and is alive. Where I've placed ###Your code here### is where you'd either assign your bonus (unit, yield, dummy policy/building etc.) or you could call a function.
This method is robust and takes effect immediately upon entering the Era unlike SerialEventEraChanged. It's also triggered when bulbing techs through Scientests and should a mod add techs that are out of sequence we're verifying those too.