Religion spreading lua failing to fire when combined with other mods

Poizen 87

Chieftain
Joined
Jan 22, 2015
Messages
68
I have a scenario mod I'd like to try with the CBP and/or some modded civs. Loading the scenario with either enabled, however, breaks the religion spreading mechanic for the scenario. The script, which founds religions in capitals in the scenario, and spreads it to every city where appropriate, looks something like this:

Code:
function FoundReligion(pCity, religion, pantheonBelief, founderBelief, followerBelief, followerBelief2, enhancerBelief)
	local iPlayer = pCity:GetOwner()
	local iReligion = GameInfoTypes[religion]

	-- Optional extra beliefs
	local iBelief4 = followerBelief2 and GameInfoTypes[followerBelief2] or -1
	local iBelief5 = enhancerBelief and GameInfoTypes[enhancerBelief] or -1

	Game.FoundPantheon(iPlayer, GameInfoTypes[pantheonBelief])
	Game.FoundReligion(iPlayer, iReligion, nil, GameInfoTypes[founderBelief], GameInfoTypes[followerBelief], iBelief4, iBelief5, pCity)
end

-- Set up the religions to be present in the scenario
function SetupReligions()

	if Game.GetElapsedGameTurns() == 0 then

		for iPlayer=0, GameDefines.MAX_MAJOR_CIVS-1, 1 do
			
			local pPlayer = Players[iPlayer];
			print("Iterating players..." .. pPlayer:GetName());

			if pPlayer:IsEverAlive() then
				
				for cityIndex = 0, pPlayer:GetNumCities() - 1, 1 do
					
					local pCity = pPlayer:GetCityByID(cityIndex)

					-- Found Catholicism
					if pCity ~= nil and pCity:GetName() == "Rome" then
						FoundReligion(pCity, "RELIGION_CHRISTIANITY", "BELIEF_FERTILITY_RITES", "BELIEF_PAPAL_PRIMACY", "BELIEF_CATHEDRALS", "BELIEF_CHORAL_MUSIC", "BELIEF_HOLY_ORDER");
						print("Founding Christianity in " .. pCity:GetName());
						--return pCity;
					end
					-- Found Protestantism
					if pCity ~= nil and pCity:GetName() == "Frankfurt" then
						FoundReligion(pCity, "RELIGION_PROTESTANTISM", "BELIEF_GOD_CRAFTSMEN", "BELIEF_TITHE", "BELIEF_RELIGIOUS_CENTER", "BELIEF_RELIGIOUS_COMMUNITY", "BELIEF_ITINERANT_PREACHERS");
						print("Founding Protestantism in " .. pCity:GetName());
						--return pCity;
					end
					-- Found Orthodoxy
					if pCity ~= nil and pCity:GetName() == "Moscow" then
						FoundReligion(pCity, "RELIGION_ORTHODOXY", "BELIEF_GOD_KING", "BELIEF_CHURCH_PROPERTY", "BELIEF_MONASTERIES", "BELIEF_FORMAL_LITURGY", "BELIEF_RELIGIOUS_UNITY");
						print("Founding Orthodoxy in " .. pCity:GetName());
						--return pCity;
					end
					-- Found Islam
					if pCity ~= nil and pCity:GetName() == "Mecca" then
						FoundReligion(pCity, "RELIGION_ISLAM", "BELIEF_DESERT_FOLKLORE", "BELIEF_PILGRIMAGE", "BELIEF_MOSQUES", "BELIEF_DIVINE_INSPIRATION", "BELIEF_DEFENDER_FAITH");
						print("Founding Islam in " .. pCity:GetName());
						--return pCity;
					end
					-- Found Hinduism
					if pCity ~= nil and pCity:GetName() == "Delhi" then
						FoundReligion(pCity, "RELIGION_HINDUISM", "BELIEF_SACRED_WATERS", "BELIEF_INTERFAITH_DIALOGUE", "BELIEF_GURUSHIP", "BELIEF_PEACE_GARDENS", "BELIEF_MESSIAH");
						print("Founding Hinduism in " .. pCity:GetName());
						--return pCity;
					end
					-- Found Buddhism
					if pCity ~= nil and pCity:GetName() == "Lhasa" then
						FoundReligion(pCity, "RELIGION_BUDDHISM", "BELIEF_ANCESTOR_WORSHIP", "BELIEF_INITIATION_RITES", "BELIEF_PAGODAS", "BELIEF_FEED_WORLD", "BELIEF_RELIQUARY");
						print("Founding Buddhism in " .. pCity:GetName());
						--return pCity;
					end
					-- Found Shintoism
					if pCity ~= nil and pCity:GetName() == "Kyoto" then
						FoundReligion(pCity, "RELIGION_SHINTO", "BELIEF_GOD_SEA", "BELIEF_CEREMONIAL_BURIAL", "BELIEF_ASCETISM", "BELIEF_SWORD_PLOWSHARES", "BELIEF_JUST_WAR");
						print("Founding Shintoism in " .. pCity:GetName());
						--return pCity;
					end
					
				end
			end
		end
	end
end

Events.SequenceGameInitComplete.Add(SetupReligions);

And then it iterates through each city in the world to spread the religion there. using a function that looks like:

Code:
if pCity ~= nil and pCity:GetName() == "Genoa" then 
						pCity:AdoptReligionFully(GameInfoTypes["RELIGION_CHRISTIANITY"]);
						print("Adding Catholicism to " .. pCity:GetName());

Now, it works completely fine without when I play the map and it's the only mod active, but as soon as I add either CBP or modded Civs, the lua simply ceases to fire correctly. In fact there's absolutely no evidence of it at all in the lua output log, not even an error attributed to it. I'm troubleshooting as best I can but coming up blank.

There are a few things I know that aren't causing the problem. It's not caused by an addition of new religions, nor beliefs as far as I can tell. And none of the cities have been renamed by loading the mods. I know it would be exceedingly difficult to pinpoint what mod is specifically causing it and how, so I'd just like some pointers for troubleshooting- what kind of things could cause that script to fail to launch?
 
This is wrong

Code:
for cityIndex = 0, pPlayer:GetNumCities() - 1, 1 do
	local pCity = pPlayer:GetCityByID(cityIndex)

GetCityByID() wants the ID of the city (your capital is usually 8196 or something like that) not the index (1st, 2nd, 3rd) in the empire

You need to use

Code:
for pCity in pPlayer:Cities() do

which will iterate all cities controlled/owned by pPlayer
 
Yeah. What I always get in the log from running a mod I have that records the cityID, turn#, playerID for a wonder when the wonder is constructed, is city ID #8192 when the wonder is constructed in a player's capital city. Other cities have ID#s as I recall that are either 4-digit or 5-digit numbers.

I haven't looked to see what you get (if any different than normal) when using a Worldbuilder Mao that has pre-placed cities.
 
This is wrong

Code:
for cityIndex = 0, pPlayer:GetNumCities() - 1, 1 do
	local pCity = pPlayer:GetCityByID(cityIndex)

GetCityByID() wants the ID of the city (your capital is usually 8196 or something like that) not the index (1st, 2nd, 3rd) in the empire

You need to use

Code:
for pCity in pPlayer:Cities() do

which will iterate all cities controlled/owned by pPlayer

I appreciate this- better code is always good. I'll give it a try yet I somehow doubt this is the solution- it would seem to me if the script itself was broken, rather than disrupted by some mod conflict, it wouldn't work at all. Yet the scenario and religion script work just fine when it's the only mod active.

Edit: Nope, that wasn't it. Script still fails to fire on turn 0 when using other mods. Works fine by itself. Time to add mods 1 by 1 to narrow down what exactly is causing it. I did notice a suspicious log entry when it's loaded with other mods though;

[9876.766] Runtime Error: Assets\DLC\Expansion2\UI\InGame\InGame.lua:1262: attempt to index local 'addinFile' (a nil value)
[9876.766] Runtime Error: Error loading Assets\DLC\Expansion2\UI\InGame\InGame.lua
 
It's not better ... your original code is making invalid assumptions. City IDs are not 1, 2, 3, 4 ... as you are assuming but (to all intents and purposes) random numbers 8192, 16348, 32689, etc
 
At this point I think a bit of background could help:

I'm basically playing a WW1 full world scenario. The religion script spreads each religion appropriately through the world. The original version of the map (Into a New Century) included stand ins for many civilizations, like Iroqouis for Canada, and I'm attempting to replace them with modded Civs from the workshop, as well as adding CBP for better immersion. Problem is, certain mods are breaking the init_religion.lua that is tied to the scenario.

Things I know so far:

-Trying to open the religion panel, at least on the initial turn, crashes the game regardless of other mods active or not, though this may be due to the sheer size of the map
-The full CBP (w/ CSD and C4D) Breaks the script
-Colonialist Legacy Civs (Canada, Australia, Mexico) break the script
-JFD Civs do not
-Other random custom Civs do not
 
I did notice a suspicious log entry when it's loaded with other mods though;

[9876.766] Runtime Error: Assets\DLC\Expansion2\UI\InGame\InGame.lua:1262: attempt to index local 'addinFile' (a nil value)

Does your mod/scenario include a custom version of InGame.lua?
 
Does your mod/scenario include a custom version of InGame.lua?

This turned out to be an unrelated error that I've fixed.

I've further narrowed down the issue further:

1. The Civ causing the problem was CL Australia (v 3)- updating to (v 4) fixed the issue, so I no longer encounter a broken script with any custom civs.

2. Community Patch alone breaks the script, so I cannot tell if CBP also breaks the script independently or not. Working correctly, the lua output looks like:

Code:
initReligion: Iterating players...Theodore Roosevelt
 initReligion: Iterating players...Franz Joseph I
 initReligion: Iterating players...Porfirio Diaz
 initReligion: Iterating players...Rodrigues Alves
 initReligion: Iterating players...Edmund Barton
 initReligion: Iterating players...Dowager Cixi
 initReligion: Founding Buddhism in Lhasa
 initReligion: Iterating players...Wilhelmina
 initReligion: Iterating players...Queen Victoria
 initReligion: Founding Hinduism in Delhi
 initReligion: Iterating players...Menelik II
 initReligion: Iterating players...Emile Loubet
 initReligion: Iterating players...Wilhelm II
 initReligion: Founding Protestantism in Frankfurt
 initReligion: Iterating players...Juilo Roca
 initReligion: Iterating players...Wilfrid Laurier
 initReligion: Iterating players...Meiji
 initReligion: Founding Shintoism in Kyoto
 initReligion: Iterating players...Gojong
 initReligion: Iterating players...Abdul Hamid II
 initReligion: Founding Islam in Mecca
 initReligion: Iterating players...Mozaffar ad-Din Shah Qajar
 initReligion: Iterating players...Carlos I
 initReligion: Iterating players...Vittorio Emanuelle III
 initReligion: Founding Christianity in Rome
 initReligion: Iterating players...Nicholas II
 initReligion: Founding Orthodoxy in Moscow
 initReligion: Iterating players...Alfonso XIII
 initReligion: Iterating players...Oscar II
 initReligion: Adding religions to the cities of Theodore Roosevelt
 initReligion: Cycling through cities.
 initReligion: Adding Protestantism to Davao
 initReligion: Cycling through cities.
 initReligion: Adding Protestantism to Cebu
 initReligion: Cycling through cities.
 initReligion: Adding Protestantism to Manila
 initReligion: Cycling through cities.
 initReligion: Adding Protestantism to Guam
 initReligion: Cycling through cities.
 initReligion: Adding Protestantism to Miami
 initReligion: Cycling through cities.
 initReligion: Adding Protestantism to Honolulu
 initReligion: Cycling through cities.
 initReligion: Adding Protestantism to New Orleans
 initReligion: Cycling through cities.
 initReligion: Adding Protestantism to Pearl Harbor
 initReligion: Cycling through cities.
 initReligion: Adding Protestantism to Los Angeles
 initReligion: Cycling through cities.
 initReligion: Adding Protestantism to Dallas
 initReligion: Cycling through cities.

With CP active, the output looks like:

Code:
initReligion: Iterating players...Theodore Roosevelt
 initReligion: Iterating players...Franz Joseph I
 initReligion: Iterating players...Porfirio Diaz
 initReligion: Iterating players...Rodrigues Alves
 initReligion: Iterating players...Edmund Barton
 initReligion: Iterating players...Dowager Cixi
 initReligion: Iterating players...Wilhelmina
 initReligion: Iterating players...Queen Victoria
 initReligion: Iterating players...Menelik II
 initReligion: Iterating players...Emile Loubet
 initReligion: Iterating players...Wilhelm II
 initReligion: Iterating players...Juilo Roca
 initReligion: Iterating players...Wilfrid Laurier
 initReligion: Iterating players...Meiji
 initReligion: Iterating players...Gojong
 initReligion: Iterating players...Abdul Hamid II
 initReligion: Iterating players...Mozaffar ad-Din Shah Qajar
 initReligion: Iterating players...Carlos I
 initReligion: Iterating players...Vittorio Emanuelle III
 initReligion: Iterating players...Nicholas II
 initReligion: Iterating players...Alfonso XIII
 initReligion: Iterating players...Oscar II
 initReligion: Adding religions to the cities of Theodore Roosevelt
 initReligion: Cycling through cities.
 initReligion: Cycling through cities.
 initReligion: Cycling through cities.
 initReligion: Cycling through cities.
 initReligion: Cycling through cities.
 initReligion: Cycling through cities.
 initReligion: Cycling through cities.
 initReligion: Cycling through cities.
 initReligion: Cycling through cities.
 initReligion: Cycling through cities.
 initReligion: Cycling through cities.
 initReligion: Cycling through cities.
 initReligion: Cycling through cities.
 initReligion: Cycling through cities.
 initReligion: Cycling through cities.
 initReligion: Cycling through cities.
 initReligion: Cycling through cities.
 initReligion: Cycling through cities.
 initReligion: Cycling through cities.
 initReligion: Cycling through cities.
 initReligion: Cycling through cities.
 initReligion: Cycling through cities.
 initReligion: Cycling through cities.

The script is actually firing now, but it isn't actually 'seeing' any of the cities, or founding the religions at all.
 
Are some of the founding cities minor civilizations? I found that I have to change change MaxActiveReligions to a ignore cities that are minor civilizations and that are holy cities. In my mod, I have four religions founded at start, but one is in a minor civilization. I was getting crashes, so I set MaxActiveReligions as "3" rather than "4" and that got rid of it.
 
Learn something every day, I guess. This:
Code:
for cityIndex = 0, pPlayer:GetNumCities() - 1, 1 do
	local pCity = pPlayer:GetCityByID(cityIndex)
	if pCity ~= nil then
		print("For cityIndex = " .. cityIndex .. " City Name is " .. pCity:GetName())
	end
end
results in this in the log (Rome actually had a bunch more cities than this, I just only copy-pasted the first ten from the log):
Code:
[515008.062] GenericScript: For cityIndex = 0 City Name is Rome
[515008.062] GenericScript: For cityIndex = 1 City Name is Lilybaeumis
[515008.078] GenericScript: For cityIndex = 2 City Name is Messana
[515008.078] GenericScript: For cityIndex = 3 City Name is Caralis
[515008.078] GenericScript: For cityIndex = 4 City Name is Thapsus
[515008.078] GenericScript: For cityIndex = 5 City Name is Segesta
[515008.078] GenericScript: For cityIndex = 6 City Name is Batavium
[515008.078] GenericScript: For cityIndex = 7 City Name is Iuvavum
[515008.078] GenericScript: For cityIndex = 8 City Name is Narbo Martius
[515008.093] GenericScript: For cityIndex = 9 City Name is Lepcis Magna
Whereas this
Code:
for cityIndex = 1, pPlayer:GetNumCities() do
	local pCity = pPlayer:GetCityByID(cityIndex)
	if pCity ~= nil then
		print("For cityIndex = " .. cityIndex .. " City Name is " .. pCity:GetName())
	end
end
results in this
Code:
[516678.296] GenericScript: For cityIndex = 1 City Name is Lilybaeumis
[516678.296] GenericScript: For cityIndex = 2 City Name is Messana
[516678.296] GenericScript: For cityIndex = 3 City Name is Caralis
[516678.296] GenericScript: For cityIndex = 4 City Name is Thapsus
[516678.312] GenericScript: For cityIndex = 5 City Name is Segesta
[516678.312] GenericScript: For cityIndex = 6 City Name is Batavium
[516678.312] GenericScript: For cityIndex = 7 City Name is Iuvavum
[516678.312] GenericScript: For cityIndex = 8 City Name is Narbo Martius
[516678.312] GenericScript: For cityIndex = 9 City Name is Lepcis Magna
[516678.312] GenericScript: For cityIndex = 10 City Name is Illyria
The city-name information is accurate for the city-names I am currently using in the game I am running. I'm testing my custom version of Rome. The number of cities and the names of each was also complete and accurate in the log as compared against what is in-game.

---------------------------------------------------------------------------------------

Which would lead to the conclusion that there are two different "CityID" systems within the game. And in this alternative "CityId" system ID# 0 will be the player's capital city, "1" will be the player's 2nd city, etc.
 
Good catch, Craig, though apparently not the issue. No, none of the holy cities are in city states, they are all owned by various civs.

CBP does in fact lower the maximum religion count though, but that does not explain why CP alone also causes the script to fail.

LeeS- from what I understand, the implications of what you're saying are that the original religion code I posted is correct?

Anyways- there has to be something in the Community Patch that's preventing the init_religion.lua from actually finding the cities- I tagged the function to print a statement "Found Rome!" before founding the religion, but after finding the city. It didn't show up in the log.
 
Good catch, Craig, though apparently not the issue. No, none of the holy cities are in city states, they are all owned by various civs.

CBP does in fact lower the maximum religion count though, but that does not explain why CP alone also causes the script to fail.

LeeS- from what I understand, the implications of what you're saying are that the original religion code I posted is correct?

Anyways- there has to be something in the Community Patch that's preventing the init_religion.lua from actually finding the cities- or prevents it from firing the foundreligion or spreadreligion function when it does, which seems bizarre and weird.
It is correct in that the method you are using to iterate through a player's cities does seem to work with the game's standard DLL.

I would try using the method everyone usually uses from the game's API:
Code:
for pCity in pPlayer:Cities() do
 
It is correct in that the method you are using to iterate through a player's cities does seem to work with the game's standard DLL.

I would try using the method everyone usually uses from the game's API:
Code:
for pCity in pPlayer:Cities() do

When I do a simple substitution into the current code, this results in a code that loops repeatedly on the same city- it'll just found Mecca a dozen times and then crash on 'next turn'. Am I missing something?
 
I was just about to then I caught the error- I hadn't deleted the Local pCity line below the 'do' statement. I must've looked at the code a hundred times and missed it each time. Sorry for wasting your time.
 
I was just about to then I caught the error- I hadn't deleted the Local pCity line below the 'do' statement. I must've looked at the code a hundred times and missed it each time. Sorry for wasting your time.

No prob. If it hadn't been so late last night I would have suggested checking that as the 1st thing to do.
 
Thanks to both you and Whoward then- it's working flawlessly now.

Now it just bugs me that pressing autoplay in firetuner 'kills' the Civ you're currently playing as (I'm running some simulations for bug-testing/curiosity).
 
Top Bottom