无法读取多维数组(表)

时间:2016-04-16 17:20:19

标签: lua roblox

    Module = {}
    tempData = {Visit = false, Stats = {Name = 'No Name', Levels = 0, XP = 0, XP2 = 10},
            Hats = {Basic = true, NightShade = false},
            Robes = {Basic = true, NightShade = false},
            Boots = {Basic = true, NightShade = false},
            Swords = {Basic = true, NightShade = false}
            }



        function Module:ChangeLook(player, category, name)
            tempData[player][category][name] = true
        end

       --[[ function Module:readStats()
            for k, v in pairs(tempData['Stats']) do
                print(k)
                print(v)
                wait()
            end
        end
        function Module:readHats()
        for k, v in pairs(tempData['Hats']) do
                print(k)
                print(v)
                wait()
        end
        end

    --These two works, but it's not effective if I have to make each function to read different array(table)]]

   --[[ function Module:ReadAll()
       for i = 1, #tempData do
           for k = 1, #tempData do
               print(tempData[i][k])
           end
       end

这个功能正是我正在努力的,但我无法弄清楚出了什么问题? 我试图读取整个数组(表)而不使用单独的函数来读取每个人。]]

    return Module

1 个答案:

答案 0 :(得分:1)

你可以在你的情况下做这样的事情:

tempData = {
  Visit = false,
  Stats = {Name = 'No Name', Levels = 0, XP = 0, XP2 = 10},
  Hats = {Basic = true, NightShade = false},
  Robes = {Basic = true, NightShade = false},
  Boots = {Basic = true, NightShade = false},
  Swords = {Basic = true, NightShade = false}
}

function ReadAll()
  for k,v in pairs(tempData) do
    if type(v) == 'table' then
      for k2, v2 in pairs(v) do
        print(k..":",k2,v2)
      end
    else
      print(k..":",v)
    end
  end
end

ReadAll()

检查它是否为表并使用键 - 值对而不是索引 - 值进行打印。