OK, the one thing with Lua that I continually struggle with is tables. I just can’t get my head around some of it.
I understand the basic table entries, it’s trying to nest them (like a database almost!)
For the love of _insert deity_ please help me understand this
For what it’s worth, I have pored over the tutorials @ coronalabs numerous times, I’ve read lua.orgs entries, I’ve read the grid entries on a post but I just don’t get it!
I am trying to work a grid. for each entry on the grid, I’d like to have more than one value available.
For example: X, Y, then Sprite, value
For grid 1, it holds sprite “dog” and has a value of 6. For grid 2 it has sprite “apple” and 4
From there, I’d like to be able to retrieve those values based on the grid (X,Y)
Here’s what I put together to test with…
It sets up the tables with the map data, and a little loop at the bottom prints out the table like the map.
I half (hope) suspect I’m most of the way there, I’m just not seeing it though.
-- working with tables local grid = {} gridsizex = 32 -- Grid is 32 x 20 gridsizey = 20 -- Grid is 32 x 20 -- Mapdata is hardcoded for now. local mapdata = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,5,5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,7,1,1,1,1,5,5,5,1,1,1,1,1,1,8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,5,5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,5,5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,5,5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,5,5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,5,5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,5,5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,5,5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,5,5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,5,5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,5,5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,5,5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,6,1,1,1,1,1,1,1,5,5,5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,5,5,1,1,1,1,1,1,1,9,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,5,5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} -- For each row (20) for mapy=1,gridsizey do -- Pretty sure this is wrong, I suspect the table declaration should be elsewhere grid[mapy] = {} end local idx = 1 for mapy=1,gridsizey do for mapx=1,gridsizex do grid[mapy][mapx] = mapdata[idx] -- 'I know this is incorrect, just trying to get the point across' -- if mapdata[idx] == 1 then -- grid[idx]["sprite"] = spr\_ground -- grid[idx]["value"] = 10 -- end -- -- if mapdata[idx] == 5 then -- grid[idx]["sprite"] = spr\_water -- grid[idx]["value"] = 15 -- end idx=idx + 1 end end -- For the X/Y grid, I want to be able to retrieve a sprite and value entry. -- print(grid[x][y][sprite]) -- print(grid[x][y][value]) print( "Printing Map" ) px="" for mapy=1,gridsizey do for mapx=1,gridsizex do px = px..grid[mapy][mapx] end print (px) px="" end