2

I am making my own game engine, using Lua C API. I got such Lua table hierarchy:

my_lib = {
  system = { ... },
  keyboard = { ... },
  graphics = { ... },
  ...
}

Also I got some C function, I want to register, something like that:

inline static int lua_mylib_do_cool_things(lua_State *L) {
    mylib_do_cool_things(luaL_checknumber(L, 1));
    return 0;
}

So, how can I register it like member of my_lib sub-table, just like that?

my_lib = {
  system = { do_cool_things, ... },
  keyboard = { ... }
  graphics = { ...}
}

Now I only know the way to register members of global tables, it works like that:

inline void mylib_registerFuncAsTMem(const char *table, lua_CFunction func, const char *index) {
    lua_getglobal(mylib_luaState, table);
    lua_pushstring(mylib_luaState, index);
    lua_pushcfunction(mylib_luaState, func);
    lua_rawset(mylib_luaState, -3);
}

But what about sub-tables?

6
  • There are API functions that can help with registering functions into tables, which version of Lua are you using? Commented Apr 13, 2016 at 17:29
  • Thanks for respond. I am using Lua 5.1, and it looks like there is no such API function there. Commented Apr 13, 2016 at 18:02
  • True, this function was added in Lua 5.2, but you might be able to use luaL_register instead. Commented Apr 13, 2016 at 18:05
  • @VasiliyPupkin Of course there are Commented Apr 13, 2016 at 18:05
  • Could you kindly write some example code? As I said, I know how to work with global tables, but not sub-ones. Commented Apr 13, 2016 at 18:14

1 Answer 1

6

A simple way to register multiple Lua C function into a table (using Lua 5.1) is to use luaL_register.

First, implement your Lua functions, they should take the form of a lua_CFunction.

static int graphics_draw(lua_State *L) {
    return luaL_error(L, "graphics.draw unimplemented");
}

static int system_wait(lua_State *L) {
    return luaL_error(L, "system.wait unimplemented");
}

Next, create a luaL_Reg structure for each sub-table with your Lua functions and their names (keys).

static const struct luaL_Reg module_graphics[] = {
    {"draw", graphics_draw},        
    // add more graphic functions here.. 
    {NULL, NULL}  // terminate the list with sentinel value
};

static const struct luaL_Reg module_system[] = {
    {"wait", system_wait},        
    {NULL, NULL}
};

Then, in the function where you return your module table create each sub-table and register its functions.

int luaopen_game(lua_State *L) {    
    lua_newtable(L);  // create the module table

    lua_newtable(L);                          // create the graphics table
    luaL_register(L, NULL, module_graphics);  // register functions into graphics table
    lua_setfield(L, -2, "graphics");          // add graphics table to module

    lua_newtable(L);                          // create the system table
    luaL_register(L, NULL, module_system);    // register functions into system table
    lua_setfield(L, -2, "system");            // add system table to module

    // repeat the same process for other sub-tables

    return 1;  // return module table
}

This should result in a module table with the following structure:

game = {
    graphics = {
        draw -- C function graphics_draw
    },
    system = {
        wait -- C function system_wait
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. After I asked the question, I found the solution myself, but yours is much more complex.
You're welcome. This pattern is typically used for Lua modules written in C, as shown in the excellent Programming in Lua book. The first edition is available online for free.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.