首页 > 解决方案 > 如何通过 C++ 全局加载 file.lua 库而无需使用:loadfile("file.lua")() 导入它?

问题描述

我在这个 github 上找到了一个名为“json.lua”的库:github json.lua

我通过 main.lua 文件导入这个库,如下所示:

local json = loadfile("json.lua")() -- json = library loaded
print("json decoded : "..json.decode("13E+2")) -- will print : json decoded = 1300.0 

但是我想全局使用“ json ”变量,而不必使用以下命令导入它: loadfile("json.lua")()

有没有办法将json.lua json.lua 文件字符串 直接全局加载到 lua VM 中,以便任何其他文件( main1.lua、main2.lua、main3.lua、...lua )我只需键入“json.lua”。 ANY_FUNCTION "然后去上班吗?

标签: c++jsonlualuac

解决方案


在加载脚本之前在 C++ 中运行它:

luaL_dostring(L,"json = dofile('json.lua')");

推荐阅读