Newb with a SDK/Worldbuilder question...

The following script works:

Code:
-- Enumerate Cities with Confucianism

--for iPlayer=0, GameDefines.MAX_MINOR_CIVS-2 do
for iPlayer=0, GameDefines.MAX_MAJOR_CIVS-1 do
	local pPlayer = Players[iPlayer] print (pPlayer:GetName())
		if pPlayer:IsEverAlive() then
			
			-- Enumerate cities
			for cityIndex = 0, pPlayer:GetNumCities() - 1, 1 do
    			local city = pPlayer:GetCityByID(cityIndex) print (city:GetName())

				-- City exists and has the proper name?
    			if city ~= nil and city:GetName() == "Roskilde" then 
					city:AdoptReligionFully(GameInfoTypes["RELIGION_CONFUCIANISM"])
				elseif city ~= nil and city:GetName() == "Oslo" then
					city:AdoptReligionFully(GameInfoTypes["RELIGION_CONFUCIANISM"])
				elseif city ~= nil and city:GetName() == "Nidaros" then
					city:AdoptReligionFully(GameInfoTypes["RELIGION_CONFUCIANISM"])
				--elseif city ~= nil and city:GetName() == "Bergen" then
					--city:AdoptReligionFully(GameInfoTypes["RELIGION_CONFUCIANISM"])
					return city							
    			end
			end
		end
end

The parts that I commented out were an attempt to use the script to include minor civilizations and their cities. That attempt failed as crashes resumed. I'm going to look further into it to see if minor civ cities are called by a different method.

I'm very satisfied now, in any case, as this is sufficient, though not necessarily fully, to meet the needs of the scenario I am working on.

Thanks for the help. I'll keep posting my progress as I continue. Hopefully, someone else will be able to use these results in their scenarios as well.
 
Minor civs are not listed because you bounded your loop to MAX_MAJOR_CIVS-1, this is the only reason. Minor civs start after that. ;)
Even if minor civs do not have a leader, they can still be enumerated the same way and the GetName function works properly for them (it returns the CS name rather than the leader's name). So, as I said, use MAX_CIV_PLAYERS - 1

Now for the crash, it's because you queried the player name before you verified IsEverAlive: if you use functions on a civ that was never present in the game (IsEverAlive returns false), it crashes. So just put that print call after the IsEverAlive test. It also provides another explanation for the minor civs not being displayed: your code crashes before that point.

So it's not like your code is stuck in an inifinte loop, it's just civ5 becoming crazy. Also you do not have to put a "return" instruction if you do not need the returned value. Besides I don't know what happens if you use return outside of a function. Nothing I guess.

Also, I suggest you put the "print" calls on their own lines, or use ";" to separate them from the previous statement. You're indeed supposed to put a line break or a ";" between two statements. And I am surprised LUA managed to interpret your code like that but I guess you will get a bad surprise someday if you keep that habit.

Finally, one advice: I suggest you use the hungarian notation (adding a type identifier prefix on names: iCity, iPlayer, pPlayer, bFlag, etc - i for integer, p for pointer/structure, b for boolean). Unfortunately I usually work with strongly typed languages through a good IDE and that makes the hungarian notation useless, but on a dynamic language like LUA it's golden. I wish I had remembered that before IGE reached thousands of lines of code.
 
Back
Top Bottom