Two-Dimensional Arrays

bane_

Howardianism High-Priest
Joined
Nov 27, 2013
Messages
1,559
I tried both


Code:
gUnitsVoidPoints = {}
for i=1, 10 do
	gUnitsVoidPoints[i] = {}
	for j=1, 10 do
		gUnitsVoidPoints[i][j] = 0
	end
end

and

Code:
gUnitsVoidPoints = {}
for i=1, 10 do
	gUnitsVoidPoints[i] = {}
	for j=1, 10 do
		gUnitsVoidPoints[i*10 + j] = 0
	end
end

But when I try to get the value (if gUnitsVoidPoints[unit:GetOwner()][unit:GetID()] ~= nil then) I get:
UnitPanel.lua:718: attempt to index field '?' (a nil value)


What am I doing wrong?
 
unit:GetID() will be much,much greater than 10!

In my current game

Code:
> for pUnit in Players[0]:Units() do print(pUnit:GetID()) end
 WorldView: 8192
 WorldView: 16385
 WorldView: 24578
 WorldView: 65539
 WorldView: 40964
 WorldView: 57349
 WorldView: 81927
 WorldView: 98312
 WorldView: 114697

Your best bet will be something like, to create the unit's void point entry as needed

Code:
local iUnit = unit:GetID()
local playerVoidPoints = gUnitsVoidPoints[unit:GetOwner()]
if (playerVoidPoints[iUnit] == nil) then playerVoidPoints[iUnit] = 0 end

-- Now you can use gUnitsVoidPoints[unit:GetOwner()][unit:GetID()]
 
I'm getting this:
Lua\Lib\UnitPanel.lua:1756: attempt to index local 'playerVoidPoints' (a nil value)

For reference, the code:
Spoiler :
Code:
--NiceTest runs after any unit is created.
gUnitsVoidPoints = {}
function NiceTest(playerID, unitID)
	local pPlayer = Players[playerID]
	if pPlayer:GetCivilizationType() == GameInfoTypes["CIVILIZATION_CRAB_CLAN"] then
		local pUnit = pPlayer:GetUnitByID(unitID)
		local pPlot = pUnit:GetPlot()
		local pCity = pPlot:GetPlotCity()
		local eInfo = GameInfo.Units[pUnit:GetUnitType()]
		local playerVoidPoints = gUnitsVoidPoints[pUnit:GetOwner()]
		if (playerVoidPoints[unitID] == nil) then 
			playerVoidPoints[unitID] = 0
		end
	end
end

I REALLY am terrible with tables.
 
That's 'cos' you no longer have the loop that creates the player arrays

From post #1 ...
Code:
gUnitsVoidPoints = {}
for i=1, 10 do
	gUnitsVoidPoints[i] = {}
end

which should actually be 0 to MAX_MAJOR_CIVS (not 1 to 10)
 
Top Bottom