I'm currently trying to implement LUA into my CPP program however I am having the following issue: I have a library in lua lets say lib.lua that I'd like to be ran into runtime so that other lua scripts can access it however I'm having issues with that here's my code:
/* initialize Lua */
L = luaL_newstate();
/* load Lua base libraries */
luaL_openlibs(L);
/* add my path to package.path */
luaL_dostring(L, "package.path = 'D://Game.Helper/Lua/libs/?.lua;'..package.path ");
/* require my the library */
luaL_dostring(L, "_G class = require 'middleclass'");
^^^ THIS IS CURRENTLY THE CODE NOT WORKING ^^^
/* load the script */
luaL_loadfile(L, "d://Game.Helper/Lua/scripts/test.lua");
lua_call(L, 0, 0);
// clean it up
lua_close(L);
the script is loading properly however if I don't require my library in the script itself it will return a nil value for it
Any help is greatly appreciated, thank you!
In Lua, when you declare a variable as local
, it's only available in that lexical scope. You did local class = require 'middleclass'
in one file, and then tried to use class
in another file, which has its own scope. You need to either get rid of local
so that class
is a global variable instead, or move that line into the file that will use class
.