[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]

Question about tables as multi-dimensional arrays Started by FlyinWhee Aug 01 2010 02:08 PM

4 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

FlyinWhee

[GLOBAL: userInfoPane.html]
FlyinWhee
• Observer

• 9 posts
• Guests

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.
uid: 4920 topic_id: 1564 reply_id: 301564

[TOPIC: post.html]
#2

FlyinWhee

[GLOBAL: userInfoPane.html]
FlyinWhee
• Observer

• 9 posts
• Guests

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?
uid: 4920 topic_id: 1564 reply_id: 4451

[TOPIC: post.html]
#3

DFox

[GLOBAL: userInfoPane.html]
DFox
• Contributor

• 139 posts
• Corona SDK

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.
uid: 6678 topic_id: 1564 reply_id: 4453

[TOPIC: post.html]
#4

FlyinWhee

[GLOBAL: userInfoPane.html]
FlyinWhee
• Observer

• 9 posts
• Guests

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 !
uid: 4920 topic_id: 1564 reply_id: 4456

[TOPIC: post.html]
#5

tokyodan

[GLOBAL: userInfoPane.html]
tokyodan
• Contributor

• 260 posts
• Guests

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)

```
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
```
uid: 295 topic_id: 1564 reply_id: 4473

[topic_controls]

[/topic_controls]