Question about tables as multi-dimensional arrays

From the LUA guide, I understand that multidimensional arrays need to be declared explicitly, arrays within arrays:

  
-- In this example, everything in caps is a variable previously declared to act as a "constant"  
  
Cards[PLAYER1] = {};  
Cards[PLAYER1][OFF] = {};  
Cards[PLAYER1][OFF][LEFT] = {};  

Now, in a traditional language, I would create a “default item” of my array, in order to initialize my array. I would basically do:

  
Cards[PLAYER1][OFF][LEFT][IMAGE] = 0;  
Cards[PLAYER1][OFF][LEFT][INDEX] = 0;  
Cards[PLAYER1][OFF][LEFT][X] = 0;  
Cards[PLAYER1][OFF][LEFT][Y] = 0;  
  

and then copy it over…

  
Cards[PLAYER1][OFF][CENTER] = Cards[PLAYER1][OFF][LEFT];  
Cards[PLAYER1][OFF][RIGHT] = Cards[PLAYER1][OFF][LEFT];  
  

I would actually use a local variable to do the copying over, for efficiency - but that is not the main point of my question.

Here’s the question:

The way I understand it, in LUA, doing that basically makes Cards[PLAYER1][OFF][CENTER] point to Cards[PLAYER1][OFF][LEFT], which means they’re basically not two seperate entries in the array, but simply two pointers to the same entry… So how would I go about doing what I want to do, in LUA

This probably sounds like one hell of a noobish question, but I’m still having a hard time getting my head around some of the concepts of LUA. [import]uid: 4920 topic_id: 1564 reply_id: 301564[/import]

I realize my question is quite long and convoluted, so let me make it simpler :

Let’s say I use a table here to work as a Record type:

 Test = {};  
 Test.P1 = {};  
 Test.P1.Off = 1;  
  
 Test.P2 = Test.P1;  
 Test.P3 = Test.P1;  
 Test.P2.Off = 5;  
 print(Test.P1.Off); -- '5'  
 print(Test.P3.Off); -- '5'  

In this example there is a single field to the record (Off), but if there were 15, would I have to assign every one directly, or is there a way to copy the ‘record’ structure and content of Test.P1, without simply having the assigned table (Test.P2, Test.P3) act as a pointer? [import]uid: 4920 topic_id: 1564 reply_id: 4451[/import]

If I understand your question, you might want to try something like this: http://lua-users.org/wiki/CopyTable

I haven’t tried it yet but I think it’s supposed to copy the actual object instead of just using a reference. [import]uid: 6678 topic_id: 1564 reply_id: 4453[/import]

This would work, if my array only had one level deep, but I’ve now realized given the size of the solution (and potentially the inefficient code it would require) needed here, I’m better off just manually initializing my arrays with a couple of loops at the beginning of the game.

Thanks for the help ! [import]uid: 4920 topic_id: 1564 reply_id: 4456[/import]

How about this code snippet which I’m using in my game?

It generates a 13 X 13 array with each cell holding a table with an x position and a y position:
(13 tables in Y axis with each table holding 13 tables in X axis, and each table holding an x position and a y position)

[code]

playfieldPhysPos = {}
local aRow = {}

local bdLogicX, bdLogicY

local colCtr = 1 – to count columns on x axis

function initPhysicalPosArray()
local y, x, bdX, bdY

for y = 1, 319, 24 do – do all physical positions on board
for x = 1, 319, 24 do

bdLogicX = math.modf(x / 24)
bdLogicY = math.modf(y / 24)

– This will snap player to physical board positions
bdX = bdLogicX * 24
bdY = bdLogicY * 24

– print(“bdLogicX = “… bdLogicX …” : x=”… bdX …"; bdLogicY = “… bdLogicY …” : y=" …bdY)

local xy = {}
table.insert(xy, bdY) – xy[1] = bdY
table.insert(xy, bdX) – xy[2] = bdX
table.insert(aRow, xy) – aRow[1] = xy[1] & xy[2]

colCtr = colCtr + 1

end – end x loop

if colCtr > 13 then – only do if row has been filled
– print("============= TABLE LENGTH: " … table.maxn(aRow))
table.insert(playfieldPhysPos, aRow)
aRow = {} – new row
colCtr = 1
end

end – end y loop

– print("============= TOTplayfieldPhysPos LENGTH: " …table.maxn(TOTplayfieldPhysPos))
end
[/code] [import]uid: 295 topic_id: 1564 reply_id: 4473[/import]