Question

local tbmoffsets = {}
local facedir = {}
local row = {}
local height = 10
local widthl = -5
local widthr = 5
local depth = 3



            Z1=1
            for Y1 = height, -1,-1 do
                for X1 = widthl,widthr do
                  row[#row + 1] = {X = X1, Y = Y1, Z = Z1}
                end 
             --facedir[Y1]=row
            end
           for m = height,-1,-1 do 
            for n = 1,#row do 
             o = row[n].Y
             if o == m then
              print(o)
              facedir[o] = row[n]
             end
            end 
           end
tbmoffsets[1] = facedir

why does this code not produce tables like this;

1
 1
  1
    X=-1    Z=1     Y=2
  2
    X=0     Z=1     Y=2
  3
    X=1     Z=1     Y=2
 2
  1
    X=-1    Z=1     Y=1
  2
    X=0     Z=1     Y=1
  3
    X=1     Z=1     Y=1
 3
  1
    X=-1    Z=1     Y=0
  2
    X=0     Z=1     Y=0
  3
    X=1     Z=1     Y=0

you'll notice that the formatting i want puts all of the similar Y variables in there own "block" (table) and when i read that code in my mind it should do it but no such luck for me, 3 days with this measly 11 lines of code and i have tried a dozen or more variations of the same code and no matter what i do to it it always messes up this current version produced results that group together only the X values which = 5 and then poof discards all of the rest of the data >:( really making me wanna beat this computer.

Results of above code:

1: 
  1: 
    X: 5
    Y: 1
    Z: 1
  2: 
    X: 5
    Y: 2
    Z: 1
  3: 
    X: 5
    Y: 3
    Z: 1
  4: 
    X: 5
    Y: 4
    Z: 1
  5: 
    X: 5
    Y: 5
    Z: 1
  6: 
    X: 5
    Y: 6
    Z: 1
  7: 
    X: 5
    Y: 7
    Z: 1
  8: 
    X: 5
    Y: 8
    Z: 1
  9: 
    X: 5
    Y: 9
    Z: 1
  10: 
    X: 5
    Y: 10
    Z: 1
  0: 
    X: 5
    Y: 0
    Z: 1
  -1: 
    X: 5
    Y: -1
    Z: 1
Was it helpful?

Solution

If you replace

   for m = height,-1,-1 do 
    for n = 1,#row do 
     o = row[n].Y
     if o == m then
      print(o)
      facedir[o] = row[n]
     end
    end 
   end

with

   for m = height,-1,-1 do
    for n = 1,#row do
     o = row[n].Y
     if o == m then
      if not facedir[o] then
          facedir[o] = {}
      end
      local t = facedir[o]
      t[#t + 1] = row[n]
     end
    end
   end

then you get values more like what you want I believe. Though you can avoid the second loop entirely if you create independent row tables and not one giant row table like below.

local tbmoffsets = {}
local facedir = {}
local height = 10
local widthl = -5
local widthr = 5
local depth = 3

local Z1=1
for Y1 = height, -1,-1 do
    local row = {}
    for X1 = widthl,widthr do
        row[#row + 1] = {X = X1, Y = Y1, Z = Z1}
    end
    facedir[Y1]=row
end

tbmoffsets[1] = facedir
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top