简体   繁体   中英

How to use in javascript some data coming from lua files?

I have some lua files that I need to read in JS. I would like to be able to import the variables (mostly tables) and go through them easily in JS, to add things in the DOM.

Exemple of LUA file:

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

There are multiple json libraries available for Lua. You can use them to serialize Lua tables into json strings.

See http://lua-users.org/wiki/JsonModules

Encoding example for dkjson:

local json = require ("dkjson")

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

print (str)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM