Search code examples
c++jsonlualuac

How can I load the file.lua library globally by C++ without having to import it with : loadfile("file.lua")()?


I have a library called " json.lua " found on this github : github json.lua

I'm importing this library through the main.lua file like this:

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

but I want to use the " json " variable globally, without having to import it with : loadfile("json.lua")()

is there any way to load json.lua OR json.lua file string directly into the lua VM globally so that any other file ( main1.lua, main2.lua, main3.lua, ...lua ) I just type " json.ANY_FUNCTION " and go work?


Solution

  • Run this in C++ before loading your scripts:

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