Lua Academy · Lesson

Calling Lua Functions from C

Load and execute Lua scripts and functions from C code.

Lesson 2 of 413 steps

Calling Lua Functions from C is a free Lua Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, and your progress syncs across the web and the CoddyKit app. The Lua Academy course includes 4 lessons in total.

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 arg2

lua_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.

Free to start

Learn Lua with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

Frequently Asked Questions

Is the “Calling Lua Functions from C” lesson free?

Yes — any 3 lessons of the Lua Academy learning path, including “Calling Lua Functions from C”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Lua Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

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

  1. The Lua C API Stack Model
  2. Calling Lua Functions from C
  3. Registering C Functions in Lua
  4. Managing State and Error Handling
← Back to Lua Academy