Loading Data to project from external Lua script

Hello all.

I have lua module with lua table for dialog system. After edit this table I need rebuild bundle everytime(because table compiled into bundle), send to my colleague for testing project, received feedback, fix table, make bundle and send again.

How make external table, json file or may be something other for change this table without need rebuild bundle?

You have many choices here. The easiest would probably to put the json file somewhere on disk and load it using io.open() and read() and parse it using json.decode(). Something like this:

--dialogs.lua

local M = {}

local dialogs = {
	-- your dialog data here
}

-- refresh dialogs from file on disk
function M.refresh()
	local file = io.open("path/to/dialogs.json", "rb")
	local data = file:read("*a")
	dialogs = json.decode(data)
end

return M
2 Likes

Thank you.

I can load plane LUA table same method from extrenal file?

No, you cannot load plain Lua tables from file. The Lua table need to be serialized/deserialized into a format suitable for storage on disk. This is for instance what sys.save() and sys.load() does. But if you want a simple human readable and easily interchangeable format that you can parse to a Lua table then json is a very good choice.

1 Like