Shaqulli
Chieftain
- Joined
- Jul 14, 2017
- Messages
- 16
I'm making a Unique Trait through a lua file.I made a function for my new civilization(CIVILIZATION_QIN).The function should have given the ARCHER unit of CIVILIZATION_QIN some certain promotions(I have defined them) according to the number of Major Civs that it is at war with.
But the function doesn't work.The record in lua.log makeme uneasy.BUt I don't what to do
I have done "InGameUIAddin".
I guess my code is wrong.
But the function doesn't work.The record in lua.log makeme uneasy.BUt I don't what to do
Code:
MODS\WarStatesOfChina (v 2)\lua/QINTrait.lua:24: attempt to call method 'IsAtWar' (a nil value
.
I guess my code is wrong.
Code:
function QINTrait(playerID)
local player = Players[playerID]
local iNumWithWar = 0
local ANTIUNITEDID = GameInfo.UnitPromotions["PROMOTION_ANTI_UNITE_D"].ID
local ANTIUNITECID = GameInfo.UnitPromotions["PROMOTION_ANTI_UNITE_C"].ID
local ANTIUNITEAID = GameInfo.UnitPromotions["PROMOTION_ANTI_UNITE_A"].ID
local ANTIUNITESID = GameInfo.UnitPromotions["PROMOTION_ANTI_UNITE_S"].ID
---------------------Run only when Qin player do turn-----------------------
if not player or not player:IsAlive() or player:GetCivilizationType()~=GameInfoTypes.CIVILIZATION_QIN then
return
end
------------------Find out the number of enemy of QIN---------------
for _,targetplayer in pairs(Players) do
if targetplayer and targetplayer:IsAlive() and not targetplayer:IsBarbarian() and targetplayer:IsAtWar(player) then
iNumWithWar = iNumWithWar + 1
end
end
-----------------Give the Archer corresponding promotion according to the QIN's enemy number-------------------
--[[ nothing for 1 enemy
"PROMOTION_ANTI_UNITE_1" for 2 enemy;
"PROMOTION_ANTI_UNITE_2" for 3 enemy;
"PROMOTION_ANTI_UNITE_3" for 4 enemy;
"PROMOTION_ANTI_UNITE_4" for 5 or more enemy]]
if (iNumWithWar == 1 or iNumWithWar == 0 ) then
return
--[[if the number of enemy of QIN is 2,ARCHER will get PROMOTION_ANTI_UNITE_1]]
elseif (iNumWithWar == 2) then
for unit in QINplayer:Units() do
if unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_ARCHER then
if not unit:IsHasPromotion(ANTIUNITEDID) then
unit:SetHasPromotion(ANTIUNITEDID,true)
end
if unit:IsHasPromotion(ANTIUNITEAID) then
unit:SetHasPromotion(ANTIUNITEAID,false)
end
if unit:IsHasPromotion(ANTIUNITECID) then
unit:SetHasPromotion(ANTIUNITECID,false)
end
if unit:IsHasPromotion(ANTIUNITESID) then
unit:SetHasPromotion(ANTIUNITEDID,false)
end
end
end
return
--[[if the number of enemy of QIN is 3,ARCHER will get PROMOTION_ANTI_UNITE_2]]
elseif iNumWithWar == 3 then
for unit in QINplayer:Units() do
if unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_ARCHER then
if not unit:IsHasPromotion(ANTIUNITECID) then
unit:SetHasPromotion(ANTIUNITECID,true)
end
if unit:IsHasPromotion(ANTIUNITEAID) then
unit:SetHasPromotion(ANTIUNITEAID,false)
end
if unit:IsHasPromotion(ANTIUNITESID) then
unit:SetHasPromotion(ANTIUNITECID,false)
end
if unit:IsHasPromotion(ANTIUNITEDID) then
unit:SetHasPromotion(ANTIUNITEDID,false)
end
end
end
return
--[[if the number of enemy of QIN is 4,ARCHER will get PROMOTION_ANTI_UNITE_3]]
elseif iNumWithWar == 4 then
for unit in QINplayer:Units() do
if unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_ARCHER then
if not unit:IsHasPromotion(ANTIUNITEAID) then
unit:SetHasPromotion(ANTIUNITEAID,true)
end
if unit:IsHasPromotion(ANTIUNITESID) then
unit:SetHasPromotion(ANTIUNITEAID,false)
end
if unit:IsHasPromotion(ANTIUNITECID) then
unit:SetHasPromotion(ANTIUNITECID,false)
end
if unit:IsHasPromotion(ANTIUNITEDID) then
unit:SetHasPromotion(ANTIUNITEDID,false)
end
end
end
return
--[[if the number of enemy of QIN is up to 5,ARCHER will get PROMOTION_ANTI_UNITE_4]]
elseif iNumWithWar == 5 or iNumWithWar > 5 then
for unit in QINplayer:Units() do
if unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_ARCHER then
if not unit:IsHasPromotion(ANTIUNITESID) then
unit:SetHasPromotion(ANTIUNITESID,true)
end
if unit:IsHasPromotion(ANTIUNITEAID) then
unit:SetHasPromotion(ANTIUNITEAID,false)
end
if unit:IsHasPromotion(ANTIUNITECID) then
unit:SetHasPromotion(ANTIUNITECID,false)
end
if unit:IsHasPromotion(ANTIUNITEDID) then
unit:SetHasPromotion(ANTIUNITEDID,false)
end
end
end
end
end
GameEvents.PlayerDoTurn.Add(QINTrait)