1
votes

ok i have this table :

 tbm.offsets = { -- facedir indexed (+1)
    { -- facedir = 0
        { -- first line
            { X = -1, Y =  2, Z =  1 },
            { X =  0, Y =  2, Z =  1 },
            { X =  1, Y =  2, Z =  1 }
        },
        { -- second line
            { X = -1, Y =  1, Z =  1 },
            { X =  0, Y =  1, Z =  1 },
            { X =  1, Y =  1, Z =  1 }
        },
        { -- third line
            { X = -1, Y =  0, Z =  1 },
            { X =  0, Y =  0, Z =  1 },
            { X =  1, Y =  0, Z =  1 }
        },
        { -- forth line
            { X = -1, Y = -1, Z =  1 },
            { X =  0, Y = -1, Z =  1 },
            { X =  1, Y = -1, Z =  1 }
        }
    },
    { -- facedir = 1
        { -- first line
            { X =  1, Y =  2, Z = -1 },
            { X =  1, Y =  2, Z =  0 },
            { X =  1, Y =  2, Z =  1 }
        },
        { -- second line
            { X =  1, Y =  1, Z = -1 },
            { X =  1, Y =  1, Z =  0 },
            { X =  1, Y =  1, Z =  1 }
        },
        { -- third line
            { X =  1, Y =  0, Z = -1 },
            { X =  1, Y =  0, Z =  0 },
            { X =  1, Y =  0, Z =  1 }
        },
        { -- forth line
            { X =  1, Y = -1, Z = -1 },
            { X =  1, Y = -1, Z =  0 },
            { X =  1, Y = -1, Z =  1 }
        }
    },
    { -- facedir = 2
        { -- first line
            { X = -1, Y =  2, Z = -1 },
            { X =  0, Y =  2, Z = -1 },
            { X =  1, Y =  2, Z = -1 }
        },
        { -- second line
            { X = -1, Y =  1, Z = -1 },
            { X =  0, Y =  1, Z = -1 },
            { X =  1, Y =  1, Z = -1 }
        },
        { -- third line
            { X = -1, Y =  0, Z = -1 },
            { X =  0, Y =  0, Z = -1 },
            { X =  1, Y =  0, Z = -1 }
        },
        { -- forth line
            { X = -1, Y = -1, Z = -1 },
            { X =  0, Y = -1, Z = -1 },
            { X =  1, Y = -1, Z = -1 }
        }
    },
    { -- facedir = 3
        { -- first line
            { X = -1, Y =  2, Z = -1 },
            { X = -1, Y =  2, Z =  0 },
            { X = -1, Y =  2, Z =  1 }
        },
        { -- second line
            { X = -1, Y =  1, Z = -1 },
            { X = -1, Y =  1, Z =  0 },
            { X = -1, Y =  1, Z =  1 }
        },
        { -- third line
            { X = -1, Y =  0, Z = -1 },
            { X = -1, Y =  0, Z =  0 },
            { X = -1, Y =  0, Z =  1 }
        },
        { -- forth line
            { X = -1, Y = -1, Z = -1 },
            { X = -1, Y = -1, Z =  0 },
            { X = -1, Y = -1, Z =  1 }
        }
    }
}

it is a static table in Lua already but i want to make it dynamic, the table lists XYZ cordinates for a number of lines of blocks, i want to be able to adjust the coordinates table based on user input, for example if they want a grid of 20 blocks 3 lines high how would i assemble the table from within a for loop? each block has an xyz and is in a row, each row is part of a block and the coordinates depend on which way the direction is facing.

what i have begun to figure out in the last 15 mins it would look something like:

    temp = {}
    temp1 = {}
    temp2 = {}
    temp3 = {}

  for facedir = 1, 4 do
   for block = 1, 5 do 
    for Line = 1,5 do
      for X1 = -5, 5 do
       for Y1 = 5,-1 do 
         for Z1 = 1, 5 do
            temp = {X = X1, Y = Y1, Z = Z1}
          end
        end
      end
       temp1{ line = temp }
    end
     temp2{block = temp1}
   end
     temp3{facedir = temp2}
  end

would the above code work? or what is a better/easy solution to implement, this code will be executed repeatedly, sometimes with he same variables sometimes with different variables.

Static Sample Table Formated with lua print script in console"

1 table: 0x95efdc8
    1 table: 0x95efe18
        1 table: 0x95f1050
            X - -1
            Y - 2
            Z - 1
        2 table: 0x95f10d0
            X - 0
            Y - 2
            Z - 1
        3 table: 0x95efb28
            X - 1
            Y - 2
            Z - 1
    2 table: 0x95efba8
        1 table: 0x95efbf0
            X - -1
            Y - 1
            Z - 1
        2 table: 0x95f1658
            X - 0
            Y - 1
            Z - 1
        3 table: 0x95f16d8
            X - 1
            Y - 1
            Z - 1
    3 table: 0x95f1758
        1 table: 0x95f1780
            X - -1
            Y - 0
            Z - 1
        2 table: 0x95f1800
            X - 0
            Y - 0
            Z - 1
        3 table: 0x95f1880
            X - 1
            Y - 0
            Z - 1
    4 table: 0x95f1900
        1 table: 0x95f1948
            X - -1
            Y - -1
            Z - 1
        2 table: 0x95f19c8
            X - 0
            Y - -1
            Z - 1
        3 table: 0x95f1a48
            X - 1
            Y - -1
            Z - 1
2 table: 0x95f1ac8
    1 table: 0x95f1b18
        1 table: 0x95f1b60
            X - 1
            Y - 2
            Z - -1
        2 table: 0x95f2308
            X - 1
            Y - 2
            Z - 0
        3 table: 0x95f2388
            X - 1
            Y - 2
            Z - 1
    2 table: 0x95f2408
        1 table: 0x95f2430
            X - 1
            Y - 1
            Z - -1
        2 table: 0x95f24b0
            X - 1
            Y - 1
            Z - 0
        3 table: 0x95f2530
            X - 1
            Y - 1
            Z - 1
    3 table: 0x95f25b0
        1 table: 0x95f25f8
            X - 1
            Y - 0
            Z - -1
        2 table: 0x95f2678
            X - 1
            Y - 0
            Z - 0
        3 table: 0x95f26f8
            X - 1
            Y - 0
            Z - 1
    4 table: 0x95f2778
        1 table: 0x95f27c0
            X - 1
            Y - -1
            Z - -1
        2 table: 0x95f2840
            X - 1
            Y - -1
            Z - 0
        3 table: 0x95f28c0
            X - 1
            Y - -1
            Z - 1
3 table: 0x95f2940
    1 table: 0x95f2990
        1 table: 0x95f29d8
            X - -1
            Y - 2
            Z - -1
        2 table: 0x95f2a58
            X - 0
            Y - 2
            Z - -1
        3 table: 0x95f2ad8
            X - 1
            Y - 2
            Z - -1
    2 table: 0x95f2b58
        1 table: 0x95f2ba0
            X - -1
            Y - 1
            Z - -1
        2 table: 0x95f2c20
            X - 0
            Y - 1
            Z - -1
        3 table: 0x95f2ca0
            X - 1
            Y - 1
            Z - -1
    3 table: 0x95f2d20
        1 table: 0x95f2d68
            X - -1
            Y - 0
            Z - -1
        2 table: 0x95f2de8
            X - 0
            Y - 0
            Z - -1
        3 table: 0x95f2e68
            X - 1
            Y - 0
            Z - -1
    4 table: 0x95f2ee8
        1 table: 0x95f2f30
            X - -1
            Y - -1
            Z - -1
        2 table: 0x95f2fb0
            X - 0
            Y - -1
            Z - -1
        3 table: 0x95f3030
            X - 1
            Y - -1
            Z - -1
4 table: 0x95f30b0
    1 table: 0x95f3100
        1 table: 0x95f3148
            X - -1
            Y - 2
            Z - -1
        2 table: 0x95f31c8
            X - -1
            Y - 2
            Z - 0
        3 table: 0x95f3248
            X - -1
            Y - 2
            Z - 1
    2 table: 0x95f32c8
        1 table: 0x95f3310
            X - -1
            Y - 1
            Z - -1
        2 table: 0x95f3390
            X - -1
            Y - 1
            Z - 0
        3 table: 0x95f3410
            X - -1
            Y - 1
            Z - 1
    3 table: 0x95f3490
        1 table: 0x95f34d8
            X - -1
            Y - 0
            Z - -1
        2 table: 0x95f3558
            X - -1
            Y - 0
            Z - 0
        3 table: 0x95f35d8
            X - -1
            Y - 0
            Z - 1
    4 table: 0x95f3658
        1 table: 0x95f36a0
            X - -1
            Y - -1
            Z - -1
        2 table: 0x95f3720
            X - -1
            Y - -1
            Z - 0
        3 table: 0x95f37a0
            X - -1
            Y - -1
            Z - 1

output from current script:

1 table: 0x95f3848
    1 table: 0x95f3870
        1 table: 0x95f3898
            X - -1
            Y - 2
            Z - 1
        2 table: 0x95f3918
            X - -1
            Y - 1
            Z - 1
        3 table: 0x95f3998
            X - -1
            Y - 0
            Z - 1
        4 table: 0x95f3a40
            X - -1
            Y - -1
            Z - 1
        5 table: 0x95f3ac0
            X - 0
            Y - 2
            Z - 1
        6 table: 0x95f3a18
            X - 0
            Y - 1
            Z - 1
        7 table: 0x95f3be0
            X - 0
            Y - 0
            Z - 1
        8 table: 0x95f3c60
            X - 0
            Y - -1
            Z - 1
        9 table: 0x95f3ce0
            X - 1
            Y - 2
            Z - 1
        10 table: 0x95f3b40
            X - 1
            Y - 1
            Z - 1
        11 table: 0x95f3e40
            X - 1
            Y - 0
            Z - 1
        12 table: 0x95f3ec0
            X - 1
            Y - -1
            Z - 1
    2 table: 0x95f3f40
        1 table: 0x95f3f68
            X - -1
            Y - 2
            Z - 1
        2 table: 0x95f3fe8
            X - -1
            Y - 1
            Z - 1
        3 table: 0x95f4068
            X - -1
            Y - 0
            Z - 1
        4 table: 0x95f4110
            X - -1
            Y - -1
            Z - 1
        5 table: 0x95f4190
            X - 0
            Y - 2
            Z - 1
        6 table: 0x95f40e8
            X - 0
            Y - 1
            Z - 1
        7 table: 0x95f42b0
            X - 0
            Y - 0
            Z - 1
        8 table: 0x95f4330
            X - 0
            Y - -1
            Z - 1
        9 table: 0x95f43b0
            X - 1
            Y - 2
            Z - 1
        10 table: 0x95f4210
            X - 1
            Y - 1
            Z - 1
        11 table: 0x95eff98
            X - 1
            Y - 0
            Z - 1
        12 table: 0x95f4568
            X - 1
            Y - -1
            Z - 1
2 table: 0x95f45e8
    1 table: 0x95f4610
        1 table: 0x95f4638
            X - -1
            Y - 2
            Z - 1
        2 table: 0x95f46b8
            X - -1
            Y - 1
            Z - 1
        3 table: 0x95f4738
            X - -1
            Y - 0
            Z - 1
        4 table: 0x95f47e0
            X - -1
            Y - -1
            Z - 1
        5 table: 0x95f4860
            X - 0
            Y - 2
            Z - 1
        6 table: 0x95f47b8
            X - 0
            Y - 1
            Z - 1
        7 table: 0x95f4980
            X - 0
            Y - 0
            Z - 1
        8 table: 0x95f4a00
            X - 0
            Y - -1
            Z - 1
        9 table: 0x95f4a80
            X - 1
            Y - 2
            Z - 1
        10 table: 0x95f48e0
            X - 1
            Y - 1
            Z - 1
        11 table: 0x95f4be0
            X - 1
            Y - 0
            Z - 1
        12 table: 0x95efa88
            X - 1
            Y - -1
            Z - 1
    2 table: 0x95efcf0
        1 table: 0x95efe98
            X - -1
            Y - 2
            Z - 1
        2 table: 0x95f0528
            X - -1
            Y - 1
            Z - 1
        3 table: 0x95f0ec8
            X - -1
            Y - 0
            Z - 1
        4 table: 0x95f1c28
            X - -1
            Y - -1
            Z - 1
        5 table: 0x95f1ca8
            X - 0
            Y - 2
            Z - 1
        6 table: 0x95f1c00
            X - 0
            Y - 1
            Z - 1
        7 table: 0x95f1dc8
            X - 0
            Y - 0
            Z - 1
        8 table: 0x95f0788
            X - 0
            Y - -1
            Z - 1
        9 table: 0x95f0808
            X - 1
            Y - 2
            Z - 1
        10 table: 0x95f1df0
            X - 1
            Y - 1
            Z - 1
        11 table: 0x95f0968
            X - 1
            Y - 0
            Z - 1
        12 table: 0x95f09e8
            X - 1
            Y - -1
            Z - 1

in the output from the script there is 12 entries, and there should only be 3, so im playing with it...

1
Your for loops should work as they have the correct syntax (lua.org/pil/4.3.4.html). However, your temp, temp1, temp2 and temp3 will also be nil as they are outside the loops that define them. Remember this is lua so you can always open the lua interpreter and experiment with this stuff on the command line! - Stratus3D
if i could find a stupid lua interpreter that i can install on debian that would give me something to play with i would, i am really good with php and lua doesnt seem that different except this table thing, i have never seen a data structure that is so , well unstructured...lol. since the temp is outside the loop that defines them that would be like, they are local variables instead of global? and then based on that couldnt i just declare them on the outside or withing the funtion? or do they have to be declared in the loop? this routine will be in a function, i dont know what or if that helps - FatBoi1942
apt-get install lua5.1 or apt-get install lua5.2. - Etan Reisner
temp1{} is a function call not table creation by the way. - Etan Reisner
That will be nil comment from @Stratus3D is not exactly correct either. Given the code above (modified to be temp = {}, etc.) the loops will be constantly overwriting the temp, temp1, etc. variables with new tables and at no point will any of them be nil. If you intended to append entries to each table then you need to do that instead of simple assignment. - Etan Reisner

1 Answers

1
votes

Modified to be syntactically and logically correct the above code looks like this:

local temp3 = {}

for facedir = 1, 4 do
    local temp2 = {}

    for block = 1, 5 do
        local temp1 = {}

        for Line = 1,5 do
            local temp = {}

            for X1 = -5, 5 do
                for Y1 = 5,-1,-1 do
                    for Z1 = 1, 5 do
                        temp[#temp + 1] = {X = X1, Y = Y1, Z = Z1}
                    end
                end
            end

            temp1[Line] = temp
        end

        temp2[block] = temp1
    end

    temp3[facedir] = temp2
end

For reference tab = {key = value} will create a table, store it in the tab variable with a key of "key" and a value of whatever is in the variable value. If you want to use a variable as the key you need to use the longer tab = {[key] = value} format for the key. The bare word version is simply syntactic sugar for tab = {["key"] = value}.