如何在 javascript 中使用从 lua 文件读取的数据?

我有一些 lua 文件需要在 JS 中读取。 我想导入变量(大多数是表格)并在 JS 中轻松处理它们,以便在 DOM 中添加元素。

LUA 文件的示例:

print("lua display text")

Civs = {
    CIVILIZATION_EGYPT = { -- lua comment 1
        Playable = true,
        SpawnYear = -3000,
        StartX = 75,
        StartY = 32,
        Table = { },
        Text = "abc",
        TableTable = { {"a"}, {"b"}, {"c"}, {"d"}, {}, {}, {}},
    };
    CIVILIZATION_INDIA = {
        Playable = true,
        SpawnYear = -3000,
        StartX = 98,
        StartY = 42,
        Table = { },
        Text = "abc",
        TableTable = { {"a"}, {"b"}, {"c"}, {"d"}, {}, {}, {}},
    };
    CIVILIZATION_BABYLON = {
        Playable = true,
        SpawnYear = -3000,
        StartX = 84,
        StartY = 41,
        Table = { },
        Text = "abc",
        TableTable = { {"a"}, {"b"}, {"c"}, {"d"}, {}, {}, {}},
     }
}
-- lua comment 2
点赞
用户2858170
用户2858170

Lua 有多个可用的 json 库。你可以使用它们将 Lua 表序列化为 json 字符串。

参见 http://lua-users.org/wiki/JsonModules

使用 dkjson 进行编码的示例:

local json = require ("dkjson")

local str = json.encode (Civs, { indent = true })

print (str)
2021-03-23 11:40:09