LUA: Any tips why this code wont work?

ww2commander

Emperor
Joined
Aug 23, 2003
Messages
1,243
Location
Australia
Have written some code to capture units in production with 1 turn left. The code seems to work from what I can tell, but when I try to print out the multi-array values the Firetuner shows no print statements. Note that Dprint is a print function which works just fine.

The section in red fails to trigger. Any suggestions as to what I am doing wrong?
Code:
function ProcessEndTurnInfos ()
	
	Dprint("FUNCTION: ProcessEndTurnInfos")
	
	g_UnitAvailableNextTurn = {}

	for playerID = 0, GameDefines.MAX_CIV_PLAYERS - 1 do
		local player = Players[playerID]		
		if ( player:IsAlive() ) then
			civID = GetCivIDFromPlayerID(playerID)
			g_UnitAvailableNextTurn[civID] = {}	-- Create array for civ's units
			Dprint ("Identifying new units that will be available next turn for player "..player:GetName())
			local index = 1
			
			for city in player:Cities() do
				local unitType = city:GetProductionUnit() -- Get any unit being produced by this city
				
				if unitType ~= -1 then
					if city:GetUnitProductionTurnsLeft(unitType) == 1 then -- Capture units that will be completed next turn
						local unitID = GameInfo.Units[unitType].ID
						g_UnitAvailableNextTurn[civID][index] = {}
						g_UnitAvailableNextTurn[civID][index].CityPlot = GetPlotKey(city:Plot())
						g_UnitAvailableNextTurn[civID][index].UnitTypeID = unitID
						g_UnitAvailableNextTurn[civID][index].TurnExpected = (Game.GetGameTurn() + 1)
						index = index + 1
					end
				end
			end

[COLOR="Red"]			for key, data in ipairs(g_UnitAvailableNextTurn) do
				for key2, unitData in ipairs(data) do
					Dprint("CityPlot: "..unitData.CityPlot.." - UnitTypeID: "..unitData.UnitTypeID.." - TurnExpected: "..unitData.TurnExpected)
				end
			end[/COLOR]
		end
	end
end
 
Did that and nothing happened :(

I was hoping some fresh sets of eyes might see what I might be missing.
 
Code:
[COLOR="Red"]			for key, data in ipairs(g_UnitAvailableNextTurn) do
				for key2, unitData in ipairs(data) do
					Dprint("CityPlot: "..unitData.CityPlot.." - UnitTypeID: "..unitData.UnitTypeID.." - TurnExpected: "..unitData.TurnExpected)
				end
			end[/COLOR]

Should that be gUnitAvailableNextTurn[CivID]? Because you have this

g_UnitAvailableNextTurn[civID] = {} -- Create array for civ's units
 
Try using pairs instead of ipairs. That will probably fix the problem.
 
The outer loop needs to pairs() (as the array is sparse), but the inner loop should be ipairs() (as the indexes are consectutive from 1)
 
From my limited experience with Lua's arrays and dictionaries I'm convinced that the person(s) who designed that system must hate programers.
 
From my limited experience with Lua's arrays and dictionaries I'm convinced that the person(s) who designed that system must hate programers.

If you use

a) {} as an array, use integers as indexes and table.insert() to add items and ipairs() as the iterator
b) {} as an association, use strings as keys and pairs() as the iterator

it's a lot easier and less silly error prone
 
Code:
local unitID = GameInfo.Units[unitType].ID

This is effectively x=x (as unitType comes from the ID column and not the Type column, despite it's name!)
 
Code:
g_UnitAvailableNextTurn[civID][index] = {}
g_UnitAvailableNextTurn[civID][index].CityPlot = GetPlotKey(city:Plot())
g_UnitAvailableNextTurn[civID][index].UnitTypeID = unitID
g_UnitAvailableNextTurn[civID][index].TurnExpected = (Game.GetGameTurn() + 1)

is forcing Lua to dereference g_UnitAvailableNextTurn[civID][index] 4 times (which is inefficient)

you could just write

Code:
g_UnitAvailableNextTurn[civID][index] = {
  CityPlot = GetPlotKey(city:Plot()),
  UnitTypeID = unitID,
  TurnExpected = (Game.GetGameTurn() + 1)
}

or if you don't like that approach

Code:
local entry = {}
entry.CityPlot = GetPlotKey(city:Plot())
entry.UnitTypeID = unitID
entry.TurnExpected = (Game.GetGameTurn() + 1)
g_UnitAvailableNextTurn[civID][index] = entry

would be as efficient
 
Thanks for the feedback guys. I agree that this language can send you crazy. There are many times when I think I have mastered it and then I come across something simple and question my skills all over again!

The UnitID thing is a left over from earlier testing when I had print statements amongst each of those array lines and needed to confirm UnitType was indeed UnitID.

whoward, your last post made me slap my head and yell 'doh! Why didn't someone tell me that sooner:wallbash: Now I need to go back and redo a whole chunk of my supply system code which I thought I had streamlined as much as possible! :cringe:
 
Back
Top Bottom