c++ - 如何使用 LuaBridge 从文件加载表格? [来自 C++ 的 Lua]

标签 c++ lua

我正在阅读 LuaBridge reference manual而且我不明白如何实际加载/读取 .lua 文件,以便我可以访问其中的表。

这是 lua 文件(从 tiled 导出):

return {
  version = "1.1",
  luaversion = "5.1",
  tiledversion = "0.16.0",
  orientation = "orthogonal",
  renderorder = "right-down",
  width = 16,
  height = 16,
  tilewidth = 32,
  tileheight = 32,
  nextobjectid = 3,
  properties = {},
  tilesets = {
    {
      name = "terrain_atlas",
      firstgid = 1,
      tilewidth = 32,
      tileheight = 32,
      spacing = 0,
      margin = 0,
      image = "../../../../Resources/Tilesets/Atlas/terrain_atlas.png",
      imagewidth = 1024,
      imageheight = 1024,
      tileoffset = {
        x = 0,
        y = 0
      },
      properties = {},
      terrains = {},
      tilecount = 1024,
      tiles = {}
    }
  },
  layers = {
    {
      type = "tilelayer",
      name = "Ground",
      x = 0,
      y = 0,
      width = 16,
      height = 16,
      visible = true,
      opacity = 1,
      offsetx = 0,
      offsety = 0,
      properties = {},
      encoding = "lua",
      data = {
        839, 840, 840, 840, 840, 840, 840, 840, 840, 840, 840, 840, 840, 840, 840, 841,
        871, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 873,
        871, 872, 872, 872, 872, 936, 872, 872, 872, 872, 872, 872, 872, 872, 872, 873,
        871, 872, 937, 872, 872, 872, 872, 872, 872, 872, 872, 872, 937, 872, 872, 873,
        871, 872, 872, 872, 872, 872, 872, 872, 872, 935, 872, 872, 872, 872, 872, 873,
        871, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 873,
        871, 872, 872, 872, 936, 872, 872, 937, 872, 872, 872, 872, 872, 872, 872, 873,
        871, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 873,
        871, 872, 872, 872, 872, 872, 872, 872, 872, 872, 935, 872, 872, 872, 872, 873,
        871, 872, 872, 872, 935, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 873,
        871, 936, 872, 872, 872, 872, 872, 872, 936, 872, 872, 872, 872, 872, 872, 873,
        871, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 873,
        871, 872, 872, 872, 937, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 873,
        871, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 935, 872, 872, 872, 873,
        871, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 872, 873,
        903, 904, 904, 904, 904, 904, 904, 904, 904, 904, 904, 904, 904, 904, 904, 905
      }
    },
    {
      type = "tilelayer",
      name = "Above",
      x = 0,
      y = 0,
      width = 16,
      height = 16,
      visible = true,
      opacity = 1,
      offsetx = 0,
      offsety = 0,
      properties = {},
      encoding = "lua",
      data = {
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 938, 0, 0, 0,
        0, 0, 0, 0, 907, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 876, 0, 0, 0, 0,
        0, 0, 939, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 874, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 939, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
      }
    }
  }
}

最佳答案

您正在寻找:

f = loadfile("foo.lua")

manual 中所述和 reference manual .它读取您的文件并在 f 变量中返回您的对象。

关于c++ - 如何使用 LuaBridge 从文件加载表格? [来自 C++ 的 Lua],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36325269/

相关文章:

c - Mac OS complie dynamic C lib error (lib function will call in lua)

c++ - 使用默认打印行为在 C++ 中重新路由 Lua 打印

c++ - 在 C++ 中,你能有一个函数来修改可变长度的元组吗?

c++ - 错误 : Cannot convert char to wchat_t*

c++ - ICU 迭代代码点

debugging - Lua 堆栈回溯中缺少函数名称

c++ - 从嵌入式lua读写C结构

compilation - 编译 lua 脚本 (*.exe) 的最简单方法...?

c++ - 函数参数的编译器优化

c++ - 使用 Visual Studio 2008 在 C++ 中链接错误