[BNW] Cheking plot's city.

Annihilator

Chieftain
Joined
Oct 17, 2018
Messages
7
How do i check in what city's are my unit is? I know how to get the owner of the plot in which my unit is, but how do i get the city's name in which area my unit is?
 
You can get the ID of the city that acquired the plot with pPlot:GetCityPurchaseID()

You'll need to check the return value for a valid city ID.

If using a modded DLL based on mine, you can use local iPlayer, iCity = pPlot:GetOwner() to get both bits of info at the same time
 
print("DeusVult")

local TraitPromotion = GameInfoTypes["PROMOTION_DEUSVULT"]
local TraitPromotion2 = GameInfoTypes["PROMOTION_DEUSVULT_2"]

function DeusVult(iPlayer)
local pPlayer = Players[iPlayer]
if (pPlayer:IsAlive()) then
for pUnit in pPlayer:Units() do
local pPlot = pUnit:GetPlot()
local pCity = pPlot:GetCityPurchaseID()
local pReligion = pCity:GetReligiousMajority()
if pUnit:IsHasPromotion(TraitPromotion) then
if pPlot:IsOwned() then
if pPlayer:GetReligionCreatedByPlayer() == pReligion then
pUnit:SetHasPromotion(TraitPromotion2, true)
end
end
end
end
end
end

Events.SerialEventUnitMove.Add(DeusVult);
GameEvents.PlayerDoTurn.Add(DeusVult);

Why ain't this working?
 

Attachments

  • Lua.txt
    97 KB · Views: 66
local pReligion = pCity:GetReligiousMajority() will cause an error (and the script to abort) if the unit is not on a plot owned by a city. You need to check pPlot:IsOwned() earlier than where it is at the moment
 
local pReligion = pCity:GetReligiousMajority() will cause an error (and the script to abort) if the unit is not on a plot owned by a city. You need to check pPlot:IsOwned() earlier than where it is at the moment
I am just feeling stupid right now... thx for help
 
print("DeusVult")

local TraitPromotion = GameInfoTypes["PROMOTION_DEUSVULT"]
local TraitPromotion2 = GameInfoTypes["PROMOTION_DEUSVULT_2"]

function DeusVult(iPlayer)
local pPlayer = Players[iPlayer]
if pPlayer:IsAlive() then
for pUnit in pPlayer:Units() do
if pUnit:IsHasPromotion(TraitPromotion) then
local pPlot = pUnit:GetPlot()
if pPlot:IsOwned() then
local pCity = pPlot:GetCityPurchaseID()
local pReligion = pCity:GetReligiousMajority()
local rReligion = pPlayer:GetReligioncreatedByPlayer()
if pReligion == rReligion then
pUnit:SetHasPromotion(TraitPromotion2)
end
end
end
end
end
end
GameEvents.UnitSetXY.Add(DeusVult);

Trying to get my units to get 20% combat bonus when in city's territory that has your religion. Decided to do it via promotion but still getting errors even after checking the pPlot:IsOwned() earlier.
 

Attachments

  • Lua.txt
    87.7 KB · Views: 73
local pCity = pPlot:GetCityPurchaseID() is an integer (city id), not a city object

you'll need local pCity = Players[pPlot:GetOwner()]:GetCityByID(pPlot:GetCityPurchaseID())

(or some such, it's too long since I did this stuff)

You can't use pPlayer, as that's the owner of the unit, not necessarily the owner of the city that owns the plot the unit is standing on.
 
local pCity = pPlot:GetCityPurchaseID() is an integer (city id), not a city object

you'll need local pCity = Players[pPlot:GetOwner()]:GetCityByID(pPlot:GetCityPurchaseID())

(or some such, it's too long since I did this stuff)

You can't use pPlayer, as that's the owner of the unit, not necessarily the owner of the city that owns the plot the unit is standing on.

Oh! Okay, got it. Not quite there yet but now i know what i did wrong. Complete noob when it comes to modding in lua so might have some more questions coming in the future. Thank you so much!
 
Top Bottom