error generated when plot iteration in off map

Craig_Sutter

Deity
Joined
Aug 13, 2002
Messages
2,773
Location
Calgary, Canada
I am getting an error in a simple iteration of 6 hexes around an improvement. I suspect it is because the middle hex of the iteration is on the map edge. I do not think it is causing any problems generally, but would like to know how I can eliminate the error...

here is the code:

Code:
-- Operates Hladir Vik Improvement

function VikCapture(iPlayer)
	local pPlayer = Players[iPlayer]
	if pPlayer:GetCivilizationType() == GameInfoTypes.CIVILIZATION_CARTHAGE then
		for plotLoop = 0, Map.GetNumPlots() - 1, 1 do
			local pPlot = Map.GetPlotByIndex(plotLoop)
			if pPlot:GetOwner() == iPlayer then
				if pPlot:GetImprovementType() == GameInfoTypes.IMPROVEMENT_VIK then
					if not pPlot:IsImprovementPillaged() then
						for i = 0, 5 do
							local pAdj = Map.PlotDirection(pPlot:GetX(), pPlot:GetY(), i);
							[COLOR="Red"]if pAdj:GetOwner() == -1 then[/COLOR]
								if pAdj:IsCoastalLand() or pAdj:GetTerrainType() == TerrainTypes.TERRAIN_COAST then
									pAdj:SetOwner(iPlayer)
								end
							end
						end
					end
				end
			end
		end
	end
end

The error is at the line outlined in red... I think due to one hex being offmap. pAdj comes out as a nil value.

How do I adjust the code so that it ignores hexes that are off the map?

I do not think it truly matters in the codes functioning, however, having an error hanging around bothers me, and I want to eliminate it if it may cause a problem in the future.

Thank-you.

(PS I borrowed the code from [BNW] Colonialist Legacies - The Wabanaki (v 1))
 
Back
Top Bottom