Vasiliy Pupkin
Vasiliy Pupkin

Reputation: 113

Insert function inside subtable using Lua C API

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?

Upvotes: 2

Views: 1245

Answers (1)

Adam
Adam

Reputation: 3103

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
    }
}

Upvotes: 6

Related Questions