Some help with this code

Pizza Guy

Chieftain
Joined
Jun 29, 2016
Messages
17
Code:
local ROAD_CULTURE = 1
local CivTypeMyCiv = GameInfoTypes.CIVILIZATION_MY_CIV

function RoadCulture(pCity)
	local iCulture = 0
	local iNumRoads = 0
		for iPlot = 0, pCity:GetNumCityPlots() - 1 do
			local pPlot = pCity:GetCityIndexPlot(iPlot)
				if pPlot and pPlot:IsRoute() and pCity:IsWorkingCityPlot(pPlot) then
					iNumRoads = iNumRoads + 1
					iRoadCulture = iCulture + ROAD_CULTURE
				end
		end
		return iCulture, iNumRoads
end	

function PlayerTurn(iPlayer)
	if iPlayer <= iMaxPlayerID then
		local pPlayer = Players[iPlayer]
		if pPlayer:GetCivilizationType() == CivTypeMyCiv then
			local iCulture = 0
			for pCity in pPlayer:Cities do
				iCulture = iCulture + RoadCulture(pCity)
			end
		end
	end
end

I am making a custom civ, and this is the code for their UA. I used the code from TPangolin's Zapotec mod as an example, since it was very similar to what I am trying to achieve. I guess I screwed up, because this does not work in game. Basically what I am trying to do is make it so that roads provide culture. I bet you could get that from reading the code though. To be honest I am really new to modding with lua, and modding in general.
 
First step is to enable logging and see what the Lua log tells you. It looks like this line is the issue:

Code:
if iPlayer <= iMaxPlayerID then

iMaxPlayerID is never defined anywhere in your code, so it will be nil, so the PlayerDoTurn will fail. You should be able to cut it (and its associated end) and everything will work fine.
 
It really won't matter much because:
  • iCulture is being calculated in function PlayerTurn and then nothing is being done with this calculated value.
  • there are no event hooks anywhere I can see that will ever cause function PlayerTurn to ever actually execute.
  • Plus function RoadCulture has multiple problems, not the least of which is this
    Code:
    iRoadCulture = iCulture + ROAD_CULTURE
    And because of the other mistakes being made, this
    Code:
    return iCulture, iNumRoads
    will send
    Code:
    0,X
    back up to function PlayerTurn where "X" will actually be the number of road tiles, but this value will not be used anyway because this line is only accepting the 1st of the two values returned from function RoadCulture:
    Code:
    iCulture = iCulture + RoadCulture(pCity)
    and has no way to access the second of the two pieces of data being returned.
 
Top Bottom