Help: Can't figure out why for loop not working

ww2commander

Emperor
Joined
Aug 23, 2003
Messages
1,243
Location
Australia
I keep getting a bad argument #1 to 'ipairs' (table expected, got nil) my code no matter what I try.

All I need to do is loop through the global table g_SeasonTurns and check if a turn is satisfied.

Can anyone tell me what I m doing wrong?


Heres the code with the bolded line being the problem:
Code:
function CheckSeasonChange(playerID)

	local player = Players[playerID]
	local iturn = Game.GetGameTurn()
	
	print("Entered CheckSeasonChange function triggered")
	print("Turn:" .. iturn)
	print("Active player is: " .. player:GetName())

[COLOR="Red"][B]	for i, seasonData in ipairs (g_SeasonTurns) do[/B][/COLOR]
		
		local iturnkey = seasonData.Turn
		local seasontype = seasonData.Type
		if iturnkey == iturn then 

			if seasontype == "Winter" then
				print("winter has arrived")

			elseif seasontype == "Spring" then
				print("spring has arrived")

			elseif seasontype == "Mud" then
				print("Muddy season has arrived")
			end
		end
	end
end

And here is what my global table looks like (short extract):
Code:
g_SeasonTurns = {
{Turn = 1, Type = "Winter"}, -- Test Value to be removed
{Turn = 2, Type = "Spring"}, -- Test Value to be removed
{Turn = 18, Type = "Mud"},
}
 
DOH!

I must always remember to provide an include line for my global defines file!
I must always remember to provide an include line for my global defines file!
I must always remember to provide an include line for my global defines file!

This is what happens when you don't see the forest for the trees as the saying goes. I was concentrating so hard on the code that I forgot to declare the global defines file containing the global table.
 
Back
Top Bottom