Troller0001
I've anxiously awaited your arrival!
Looking at this code, I can't help but feel it's terribly and blasphemously incorrect, but I'm not experienced enough with Lua to know for sure:
Can I get some input?
Most of your variable names have correct syntax but many modders have adopted Hungarian Notation.
Basically, since a variable can have any type (E.g. integer, string, table, etc.), it can make code more readable to explicitly mention this type in the variable name.
For instance, a variable named pUnit will contain a pointer to a unit (i.e. you can use methods such as :GetName() or :GetUnitType() on such a variable).
sUnit will contain a string related to a unit (for instance a name)
iUnit will contain an integer related to a unit (for instance an ID)
tUnit will contain a table related to a unit, etc.
Therefore, following Hungarian Notation, your variable can rename the following variables:
Code:
iCivilisationID = GameInfoTypes["CIVILIZATION_SERTAO"]
iUAPromotion = GameInfoTypes["PROMOTION_SERTAO_RELIGION_PROMOTION"]
bHasCreatedReligion = pPlayer:HasCreatedReligion()
iPlayerReligion = pPlayer:GetReligionCreatedByPlayer()
iCityReligion = pCity:GetReligiousMajority()
pUnit = pPlayer:GetUnitByID(UnitID)
pPlot = pUnit:GetPlot()
iCity = pPlot:GetCityPurchaseID()
What is a problem is that you try to define several variables outside of the methods:
Code:
local HasCreatedReligion = player:HasCreatedReligion()
local PlayerReligion = player:GetReligionCreatedByPlayer()
local CityReligion = City:GetReligiousMajority()
local Unit = player:GetUnitByID(UnitID)
local Plot = Unit:GetPlot()
local City = Plot:GetCityPurchaseID()
If we move some of these variables of your original function, we obtain the following.
At this point, the game knows which values (most of) these variables have
Code:
function CombatBonus(iPlayer, iUnit)
local pPlayer = Players[iPlayer]
if pPlayer:GetCivilizationType() == iCivilisationID then
if pPlayer:HasCreatedReligion() then
-- Note that this will not work as pCity is not defined!
if pPlayer:GetReligionCreatedByPlayer() == pCity:GetReligiousMajority() then
-- Note that pUnit is not defined!
pUnit:SetHasPromotion(UAPromotion, true)
else
pUnit:SetHasPromotion(UAPromotion, false)
end
end
end
end
Your base logic seems good, though you're attempting to look at a city that has not been used anywhere before.
Hence, we need a way to obtain a city, which we can do based on the plot the unit is currently standing on:
Code:
-- This assumes we know pUnit
local pPlot = pUnit:GetPlot()
local iCity = pPlot:GetCityPurchaseID() -- I do not know for sure what :GetCityPurchaseID(..) will return if standing on neutral lands (I assume it's either nil or -1)
if iCity and iCity > 0 then
local pCity = pPlayer:GetCityByID(iCity)
-- Do logic with pCity here
end
The same goes for a unit, which we can obtain by:
Code:
-- This assumes we have pPlayer and iUnit
local pUnit = pPlayer:GetUnitByID(iUnit)
Combining the things gives us the following (along with a small trick to avoid code repetition):
Code:
iCivilisationID = GameInfoTypes["CIVILIZATION_SERTAO"]
iUAPromotion = GameInfoTypes["PROMOTION_SERTAO_RELIGION_PROMOTION"]
function CombatBonus(iPlayer, iUnit)
local pPlayer = Players[iPlayer]
local pUnit = pPlayer:GetUnitByID(iUnit)
local bGrantPromotion = false -- Indicates whether the unit will gain/lose the promotion
if pPlayer:GetCivilizationType() == iCivilisationID then
if pPlayer:HasCreatedReligion() then
local pPlot = pUnit:GetPlot()
local iCity = pPlot:GetCityPurchaseID() -- I do not know for sure what :GetCityPurchaseID(..) will return if standing on neutral lands (I assume it's either nil or -1)
if iCity and iCity > 0 then
local pCity = pPlayer:GetCityByID(iCity)
if pCity and pPlayer:GetReligionCreatedByPlayer() == pCity:GetReligiousMajority() then
-- A small print-statement that is useful for testing purposes.
-- Probably a good idea to comment out or remove upon release, as it'll get printed MANY times
print("Adding promotion since "..pCity:GetName().." and "..pPlayer:GetName().." share their religion")
bGrantPromotion = true
end
end
end
pUnit:SetHasPromotion(iUAPromotion, bGrantPromotion)
end
end
if IsCivActive(civilisationID) then
GameEvents.UnitSetXY.Add(CombatBonus)
end