在for-do循环结束后获取表值

时间:2014-06-21 20:09:43

标签: for-loop multidimensional-array lua

我编写了以下函数,在屏幕上绘制网格(在X-Plane 10中使用FlyWithLua)。

它可以工作并绘制网格,盒子/标签和所有内容,显然它会创建" lt_box "三维数组,当我在循环中打印时(注释掉的打印指令),它打印正确的值。

问题是,如果我从代码的任何其他部分调用lt_box [x] [y] [z],我就无法获得这些值......

任何想法?

function draw_lt_table()
    idx = 1
    idx2 = 1
    xx1 = rth_box[1]
    xx2 = rth_box[2]
    yy1 = rth_box[3]
    yy2 = rth_box[4]
    lite_idx = 1
    cell_x_size = 38
    cell_y_size = 40

    num_cols = 6
    num_rows = 6

    lt_box = {}

        if d_tab==false or d_stg==false then
            return lt_box
        end

        for idx2 = 1,num_rows,1 do
            for idx = 1,num_cols,1 do
                lt_box[idx] = {}
                lt_box[idx][idx2] = {xx1+((cell_x_size*idx)-cell_x_size), xx1+(cell_x_size)*idx, 1+yy2+((cell_y_size*idx2)-cell_y_size), 1+yy2+(cell_y_size)*idx2}  
                graphics.set_color(0, 0, 0, 0.7)
                graphics.draw_rectangle(lt_box[idx][idx2][1], lt_box[idx][idx2][3], lt_box[idx][idx2][2], lt_box[idx][idx2][4])
                graphics.set_color(1, 1, 1, 0.7)
                graphics.set_width(2)
                graphics.draw_line(lt_box[idx][idx2][1],lt_box[idx][idx2][3],lt_box[idx][idx2][1],lt_box[idx][idx2][4])
                graphics.draw_line(lt_box[idx][idx2][1],lt_box[idx][idx2][4],lt_box[idx][idx2][2],lt_box[idx][idx2][4])
                graphics.draw_line(lt_box[idx][idx2][2],lt_box[idx][idx2][4],lt_box[idx][idx2][2],lt_box[idx][idx2][3])
                graphics.draw_line(lt_box[idx][idx2][2],lt_box[idx][idx2][3],lt_box[idx][idx2][1],lt_box[idx][idx2][3])
                light_up(unpack(lt_box[idx][idx2]))
                glColor4f(1, 1, 1, 0.7)
                draw_string(lt_box[idx][idx2][1]+12, lt_box[idx][idx2][3]+30, lite_idx)
                draw_string(lt_box[idx][idx2][1]+3, lt_box[idx][idx2][3]+15, lites_vars[4][lite_idx])
                draw_string(lt_box[idx][idx2][1]+3, lt_box[idx][idx2][3]+5, lites_vars[lt_set][lite_idx])
                lite_idx = lite_idx+1 -- this is usd in line 115-116 to select values as labels 
            --[[
            print("LT_BOX " .. idx .. " " .. idx2 .. " x1 ".. lt_box[idx][idx2][1])
            print("LT_BOX " .. idx .. " " .. idx2 .. " x2 ".. lt_box[idx][idx2][2])
            print("LT_BOX " .. idx .. " " .. idx2 .. " y1 ".. lt_box[idx][idx2][3])
            print("LT_BOX " .. idx .. " " .. idx2 .. " y2 ".. lt_box[idx][idx2][4])
            print("****************************************************************************")
            --]]
            end
        --print("____________________________________________________________________________")
        end

end

1 个答案:

答案 0 :(得分:4)

内部循环中有lt_box[idx] = {},这意味着每次添加元素时都会重置该表。您必须在外部循环中创建该表。

for idx = 1,num_cols,1 do
  lt_box[idx] = {}
  for idx2 = 1,num_rows,1 do