Calling Lua Functions from C
Load and execute Lua scripts and functions from C code.
Calling Lua Functions from C is a free Lua Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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);
}Getting a Global Function
Use lua_getglobal(L, "funcName") to push a Lua global function onto the stack.
// C code
lua_getglobal(L, "myFunction"); // push function
lua_pushnumber(L, 10); // push arg1
lua_pushnumber(L, 20); // push arg2lua_pcall
lua_pcall(L, nargs, nresults, errfunc) calls the function on the stack. On success returns LUA_OK and leaves results on the stack.
// C code
if (lua_pcall(L, 2, 1, 0) != LUA_OK) {
fprintf(stderr, "Call error: %s\n", lua_tostring(L, -1));
lua_pop(L, 1);
} else {
double result = lua_tonumber(L, -1);
lua_pop(L, 1);
printf("Result: %f\n", result);
}Multiple Return Values
Specify nresults in lua_pcall to read multiple return values. They are pushed on the stack in order.
// C: calling a Lua fn that returns two values
lua_getglobal(L, "minmax");
lua_pushnumber(L, 5); lua_pushnumber(L, 3);
lua_pcall(L, 2, 2, 0);
double mn = lua_tonumber(L, -2);
double mx = lua_tonumber(L, -1);
lua_pop(L, 2);LUA_MULTRET
Pass LUA_MULTRET as nresults to accept all return values from the function. Check lua_gettop(L) afterwards to know how many were returned.
Error Handler
Pass a non-zero errfunc to lua_pcall for a custom error handler. The handler receives the error object and can add traceback info.
// C: setup traceback handler
lua_getglobal(L, "debug");
lua_getfield(L, -1, "traceback");
lua_remove(L, -2); // remove debug table
int errhandler = lua_gettop(L);
// push fn and args, then:
lua_pcall(L, nargs, nresults, errhandler);Calling Methods on Objects
To call a Lua object method: push the method, push the object (self), push other args, then call.
Passing Tables as Arguments
Create a table on the stack with lua_newtable, populate fields with lua_setfield, then include it as a function argument.
Reference Registry
Use luaL_ref(L, LUA_REGISTRYINDEX) to store a Lua value in the registry and get an integer key to retrieve it later.
// C: store a callback for later use
lua_pushvalue(L, callbackIdx);
int ref = luaL_ref(L, LUA_REGISTRYINDEX);
// Later: lua_rawgeti(L, LUA_REGISTRYINDEX, ref);Cleaning Up
Always pop unused stack values. Unbalanced stacks accumulate over many calls and eventually overflow. Check the stack size in debug builds.
lua_pcall Question
What does lua_pcall(L, 2, 1, 0) do?
Recap: Calling Lua from C
Push function + arguments, call lua_pcall, check the return code, then read results from the stack. Use luaL_ref to store Lua callbacks for later. Always pop unused stack values.
Frequently asked questions
Is the “Calling Lua Functions from C” lesson free?
Yes — the full text of “Calling Lua Functions from C” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.
What will I learn in “Calling Lua Functions from C”?
Load and execute Lua scripts and functions from C code. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Lua Academy?
No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Calling Lua Functions from C” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Lua Academy lesson?
Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
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