Convert city to Player Religion on found/conquest.

JFD

Kathigitarkh
Joined
Oct 19, 2010
Messages
9,132
Location
The Kingdom of New Zealand
I'm trying to get the lua from this thread: http://forums.civfanatics.com/showthread.php?t=498519 to work, but am not sure what I'm doing wrong.

Spoiler :
function convertCityOnCapture(oldPlayerID, capital, iX, iY, newPlayerID, conquest1, conquest2)
print("In the convertCityOnCapture code");
player = Players[newPlayerID];
// Make sure the conqueror has the trait and has founded a religion
if(player:GetCivilizationType() == GameInfoTypes["CIVILIZATION_SPAIN"] and player:GetReligionCreatedByPlayer() >= 1) then
// get the city object
city = Map.GetPlot(iX, iY).GetPlotCity();
city:AdoptReligionFully(player.GetReligionCreatedByPlayer());
end
end

GameEvents.CityCaptureComplete.Add(convertCityOnCapture)


In the log file it says:

Runtime Error: Spain/ConvertOnCapture.lua:7: attempt to index global 'player' (a nil value)

I'm not sure what this means and cannot find any information on the wiki.

Once this is working, I'd like to change it over to convert when a new city is founded instead, which I'd appreciate advice towards.

I'd appreciate any help at all.
 
Zip the built mod (from the MODS sub-directory, not the ModBuddy project) and attach it to your post - that way we don't have to guess what else is in the mod that may be affecting things or type the code into one of our own mods to test it.
 
The lines:

player = Players[newPlayerID];
...
city = Map.GetPlot(iX, iY).GetPlotCity();

Should be:

local player = Players[newPlayerID];
...
local city = Map.GetPlot(iX, iY).GetPlotCity();

A basic mistake on my part when I quickly expanded from pseudo-code to functional code. As for expanding it to work on newly founded cities, that should be simple. There is an event that fires when a city is founded (I don't remember the exact name, because there are two similarly named events and one isn't the one you want). Just add the function to the cityCreated event.
 
According to my testing, it should be this:

function convertCityOnCapture(oldPlayerID, capital, iX, iY, newPlayerID, conquest1, conquest2)
local player = Players[newPlayerID];
if(player:GetCivilizationType() == GameInfoTypes["CIVILIZATION_SPAIN"] and player:GetReligionCreatedByPlayer() >= 1) then
local city = Map.GetPlot(iX, iY):GetPlotCity();
city:AdoptReligionFully(player:GetReligionCreatedByPlayer());
end
end

where you used a period for player:GetReligionCreatedByPlayer()), using a colon proved more successful.

That said, the function seems to not give the religion to the captured city, but rather to give a follower of that religion which exerts no pressure, meaning that city can't become religious without the aide of a Missonary, like usual.

Thanks for your help anyway.
 
Check FramedArchitects Andean Scenario. I believe he had a convert on capture ability for the Spanish.
 
I don't recommend using AdoptReligionFully. I had some issues with it.

I recently made civilization converting on capture, but here I made function for city found:
Spoiler :
GameEvents.PlayerCityFounded.Add(function(player, cityX, cityY)
local aPlot = Map.GetPlot(cityX, cityY);
local aCity = aPlot:GetPlotCity();
local aPlayer = Players[player];
if ( aPlayer:GetCivilizationType() == GameInfoTypes.CIVILIZATION_KINGDOM_OF_JERUSALEM_MOD and aPlayer:GetReligionCreatedByPlayer() > 0 ) then
local relHol = aPlayer:GetReligionCreatedByPlayer();
aCity:ConvertPercentFollowers(relHol, -1, 100);
end
end)


Runtime Error: Spain/ConvertOnCapture.lua:7: attempt to index global 'player' (a nil value)
It is impossible to make something equal nothing (nil). It shows that you have messed something with 'player' variable. Most common: player = Player[ID]. Lack of 's'.
Spain/ConvertOnCapture.lua:7: <-- It indicates the line. Use ctrl + G, 7, to fast move to the problematic part of code.
 
Thanks, this worked perfectly well. I was able to figure how to reverse engineer it to do the conquered cities, as well as to give a small amount of faith every time this occurs. I will credit you and Machiavelli for the help where ever I end up using it.
 
How would I do this? I need to know. This is all I've got so far, and it's not working:
Spoiler :
Code:
GameEvents.PlayerCityCaptured.Add(function(player, cityX, cityY)
	local aPlot = Map.GetPlot(cityX, cityY);
		local aCity = aPlot:GetPlotCity();
			local aPlayer = Players[player];
				if ( aPlayer:GetCivilizationType() == GameInfoTypes.CIVILIZATION_KINGDOM_OF_JERUSALEM and aPlayer:GetReligionCreatedByPlayer() > 0 ) then
			local relHol = aPlayer:GetReligionCreatedByPlayer();
		aCity:ConvertPercentFollowers(relHol, -1, 100);
	end
end)
 
Try this:

Code:
function ConqueredCityConvertPercent(iOldOwner, bIsCapital, iCityX, iCityY, iNewOwner, iPop, bConquest)
  local newOwner = Players[iNewOwner]
  local oldOwner = Players[iOldOwner]
	if newOwner:GetCivilizationType() == GameInfoTypes.CIVILIZATION_KINGDOM_OF_JERUSALEM then
		local city = Map.GetPlot(iCityX, iCityY):GetPlotCity()
		local religionFoundedID = newOwner:GetReligionCreatedByPlayer()
		local capitalReligionID = newOwner:GetCapitalCity():GetReligiousMajority()
		local convertpercentage = 100
		if religionfoundedID > 0 then
			city:ConvertPercentFollowers(religionFoundedID, -1, convertpercentage);
		elseif capitalreligionID > 0 then
			city:ConvertPercentFollowers(capitalReligionID, -1, convertpercentage);
		end
	end
end
GameEvents.CityCaptureComplete.Add(ConqueredCityConvertPercent)
 
How would I make it so I receive a small amount of faith when this happens? Thanks in advance.
 
After city:ConvertPercentFollowers, add in player:ChangeFaith(X)

So:

Code:
if religionfoundedID > 0 then
   city:ConvertPercentFollowers(religionFoundedID, -1, convertpercentage);
   newOwner:ChangeFaith(X)
elseif capitalreligionID > 0 then
   city:ConvertPercentFollowers(capitalReligionID, -1, convertpercentage);
   newOwner:ChangeFaith(X)
end

Where X is, put in the desired amount.
 
So, like this:
Code:
function ConqueredCityConvertPercent(iOldOwner, bIsCapital, iCityX, iCityY, iNewOwner, iPop, bConquest)
  local newOwner = Players[iNewOwner]
  local oldOwner = Players[iOldOwner]
	if newOwner:GetCivilizationType() == GameInfoTypes.CIVILIZATION_KINGDOM_OF_JERUSALEM then
		local city = Map.GetPlot(iCityX, iCityY):GetPlotCity()
		local religionFoundedID = newOwner:GetReligionCreatedByPlayer()
		local capitalReligionID = newOwner:GetCapitalCity():GetReligiousMajority()
		local convertpercentage = 100
		if religionfoundedID > 0 then
			city:ConvertPercentFollowers(religionFoundedID, -1, convertpercentage);
		elseif capitalreligionID > 0 then
			city:ConvertPercentFollowers(capitalReligionID, -1, convertpercentage);
if religionfoundedID > 0 then
   city:ConvertPercentFollowers(religionFoundedID, -1, convertpercentage);
   newOwner:ChangeFaith(50)
elseif capitalreligionID > 0 then
   city:ConvertPercentFollowers(capitalReligionID, -1, convertpercentage);
   newOwner:ChangeFaith(50)
end
		end
	end
end
GameEvents.CityCaptureComplete.Add(ConqueredCityConvertPercent)
 
Here is the code I use for the Crusades belief, which gives 25 faith and converts a quarter of the majority religion's followers to the player's religion when they capture a city. The red section will need to be changed and I've deleted some stuff you won't need.

Code:
function Crusades(hexPos, playerID, cityID, newPlayerID)
	local player = Players[newPlayerID];
	local eReligion = player:GetReligionCreatedByPlayer();
	local plot = Map.GetPlot(ToGridFromHex(hexPos.x, hexPos.y));
	local city = plot:GetPlotCity();
	
	if([COLOR="Red"]player is not civ[/COLOR] or eReligion < 1) then
		return;
	end

	-- Only run this if the city has a religion, and the city is of a different religion
	if (plot:IsCity() and 0 < city:GetReligiousMajority() and eReligion ~= city:GetReligiousMajority()) then
		-- Convert 25% of the heathens
		[COLOR="Green"]city:ConvertPercentFollowers(eReligion, city:GetReligiousMajority(), 25);
[/COLOR]
		-- Give bonus Faith
		[COLOR="Green"]local bonusFaith = 25;[/COLOR]
		local adjustedBonusFaith = (bonusFaith * GameInfo.GameSpeeds[Game:GetGameSpeedType()].FaithPercent) / 100; -- Adjust the size of the bonus based on the game speed
		player:ChangeFaith(adjustedBonusFaith);
	end
end
Events.SerialEventCityCaptured.Add(Crusades);
 
Back
Top Bottom