Calling Lua Functions from C
Load and execute Lua scripts and functions from C code.
Overview
To call a Lua function from C: push the function, push arguments, call lua_pcall, then read the results from the stack.
Loading a Lua Script
luaL_dofile(L, filename) loads and executes a Lua file. Or use luaL_loadfile + lua_pcall for separate load/call.
// C code
if (luaL_dofile(L, "config.lua") != LUA_OK) {
fprintf(stderr, "Error: %s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}All lessons in this course
- The Lua C API Stack Model
- Calling Lua Functions from C
- Registering C Functions in Lua
- Managing State and Error Handling