Loading a Lua file
To read a Lua variable in C, you will need to load the Lua file with int luaL_loadfile(lua_State*, const char*). The resulting chunk will then need to be executed with lua_pcall(lua_State*, int, int, int, int). After the Lua chunk is loaded and executed, any variables it declared can be read. The lua_pcall function will be described in more detail in the Calling Lua functions from C section of this chapter.
The luaL_loadfile function returns 0 on success, or one of the following enumerations on failure: LUA_ERRSYNTAX, LUA_ERRMEM, LUA_ERRFILE. The first argument to the function is the Lua state to work with, the second is the file path to load. The resulting Lua chunk is added to the Lua stack.
The lua_pcall function is ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access