Page 1 of 1

Tables within tables, within tables...

Posted: Wed Mar 06, 2013 11:47 pm
by Luska72
I am trying to make a 2d, overhead-view game and I am having some problems with my map.

I want to create a map table that has an [x] and [y] dimension. However, I also want the map to be in layers. This way I could make layer 1 be the basement level, 2 be the floor, and 3 be a roof (or something similar to that). Unfortunately, love seems to crash every time I try to make one.

After some online research, I read that this should work:

Code: Select all

Table = { {{1,1,1},{2,2,2},{3,3,3}}, {{4,4,4},{5,5,5},{6,6,6}} }
For some reason, when I put this line of code anywhere in my project it force closes! I don't even get an error screen, it just closes :(.
...
After some more experimenting I found I the only way to form 3d+ tables (without crashing) was with this code:
(Code found on: http://www3.roblox.com/Forum/ShowPost.a ... D=46861712)

Code: Select all

level = { }

for i= 1, 3 do -- 18
level[i] = { }
	for j = 1, 18 do -- 
        level[j] = { }
        for m = 1, 18 do -- 
        	level[m] = { }
        	end
    end
end


But when I try to call on it, It doesn't work! I've tried everything I can think of, including:

Code: Select all

level[x][y][z]

Code: Select all

local tempOne = level[x]
local tempTwo = tempOne[x]
local tempThree = tempTwo[x]
value = tempThree[x]

Code: Select all

for i,v in ipairs(level) do
        for it, vt in ipairs(i[x]) do
                return vt
        end
end
No matter what I try, I can't seem to find a solution that doesn't crash the program.

Anyway, if anybody knows how to make (and call from) 3d tables I would be very grateful. Google has a lot of working 2d examples, but no 3d ones that don't crash my game.

Thank you for taking the time to read this, I promise I spent a while trying to figure this out before coming to the forums :).

Re: Tables within tables, within tables...

Posted: Thu Mar 07, 2013 12:12 am
by pakoskyfrog
Hellö !

The first thing i can see in your code is that you don't initialize where you should.

Think of a multidimensionnal table as a nested lists. a 2D table is a list of list, a 3D is a list of 2D table, ...

So when you want to init a 3D table you can do this :
I took the example of a volume, with layers. Each layer is a 2D table with column stored inside ligns.

Code: Select all

local volume = {}
for i = 1, 10 do
    volume[i] = {} -- here, init of the i-th layer
    for j = 1, 15 do
        -- here, you init the j-th line inside your i-th layer
        volume[i][j] = {}
        for k = 1, 20 do
            -- and here you init the last thing, your column inside your line inside your layer
            volume[i][j][k] = math.random(17,33) -- or whatever you need here
        end
    end
end
Be careful though, i,j and k refer to the layer, the line and the column. Alway access what you stored in "volume" in this order, if you have a reference system {layer, x, y} you need to access it by volume[layer][y][x].

FYI, the informations are stored insides the columns, inside the list of layers and the lists of lines there is only table references or pointers.

EDIT :
You can access this also with the ipairs iterator like so,
(if you need to access all of it ):

Code: Select all

for floorNbr, layer in ipairs(volume[i]) do
    for lineNbr, line in ipairs(layer) do
        for colNbr, info in ipairs(line) do
            print(info) -- or whatever you need to do with it
        end
    end
end
(or just a specific layer) :

Code: Select all

local layer = volume[4]
for y, line in ipairs(layer) do
    for x, info in ipairs(line) do
        print(tostring(info)..' at '..x..":"..y) -- or whatever you need to do with it
    end
end

Re: Tables within tables, within tables...

Posted: Thu Mar 07, 2013 12:34 am
by Luska72
Thank you very much the the response! I'm can't believe I didn't figure that out :P. Now my map will have layers! Maybe I'll make a bridge! Maybe I'll make a staircase! Thanks again, and hopefully I'll have a cool-bean map to show off soon!