I'd like to use Lua to write some scripts which should allow me, given the state of certain variables of a specific instance, to determine a number to be assigned to a variable of that instance.

Let me explain: I have a class, which is called Monster. This class contains a boolean variable which is called "Slowed". My script should return a different number depending on Slowed's value.

To do this, I have to push a lot of functions in Lua, for example getSlowed(), and this functions also depend on the kind of instance.

So, I'd like to write a "LuaManager" class which can push the functions in Lua's context... the problem is that I don't know where to start. Anyone has any ideas, or a guide to suggest which I can read?


  • \$\begingroup\$ Are you asking about which Lua API calls are needed to make a C++ function available to Lua? Or some other question? \$\endgroup\$ Commented Dec 14, 2011 at 20:23
  • \$\begingroup\$ No, let my clarify, i have to call a lot of functions so i'm asking in wich way i could manage all my getters and setters for every istance that i have! :) \$\endgroup\$
    – Anonymous
    Commented Dec 14, 2011 at 20:38

2 Answers 2


Writing C++ bindings for Lua is a little more complicated than writing regular C bindings generally. There are things like LuaBind which can automate the process which some people find useful. If you just want to learn how to do simple Lua to C bindings by hand, you should read this (and keep this page handy too). Even if you're not going to do it by hand and decide to go with an automated approach, you should read it anyway so you can understand what's going on under the hood.

Writing bindings for C++ is harder for a few reasons. Lua only uses void*s to store your C/C++ objects, so you lose a lot of your type safety. There are also idioms in C++ like RAII that don't translate to Lua as well.

However, if you want to write your own lua bindings for C++ like I did, you can use LuaWrapper. I wrote it for my own game but I put it online in case others find it useful. It handles some of the issues I was running into when using C++ and Lua together, like ensuring that C++ objects are garbage collected properly and making sure when you pass them back and forth between C++ and Lua you always have the right type. You'll need to understand how Lua and regular C fit together to use it though. You can look at this example project I put up that shows how to use LuaWrapper to bind to C++ objects in Lua.

Since you mention getters and setters, I'll also mention I just recently added a second file to LuaWrapper that contains a bunch of common patterns I was using in my code (this isn't in the example project yet). For example, I have a C++ object called a Style which represents how a UI widget will look in various states. To write the getters and setters for that objcet in C++ all I needed to do was add this to create getters and setters in Lua:

static luaL_reg Style_Metatable[] =
    //                              Type   Member type    Field
    { "idle",           luaU_getset<Style, TextureGroup, &Style::Idle>           },
    { "hover",          luaU_getset<Style, TextureGroup, &Style::Hover>          },
    { "pressed",        luaU_getset<Style, TextureGroup, &Style::Pressed>        },
    { "focusedIdle",    luaU_getset<Style, TextureGroup, &Style::FocusedIdle>    },
    { "focusedHover",   luaU_getset<Style, TextureGroup, &Style::FocusedHover>   },
    { "focusedPressed", luaU_getset<Style, TextureGroup, &Style::FocusedPressed> },
    { "disabled",       luaU_getset<Style, TextureGroup, &Style::Disabled>       },
    { NULL, NULL }

int luaopen_Style(lua_State* L)
    luaW_register<Style>(L, "Style", NULL, Style_Metatable);
    return 1;

That allows me to do this in Lua:

buttonStyle = Style.new()
  • \$\begingroup\$ Wow, great work ... yes my goal is to write my lua binder, your code could help me a lot! Another simple question... if I push a class or function in lua context, is it possible to pop it from the context later? \$\endgroup\$
    – Anonymous
    Commented Dec 15, 2011 at 23:51
  • \$\begingroup\$ Depends on what you mean. If you're talking about C/C++ functions, generally you load those into a table, like my Style table in the example. If you wanted, you could use the lua API to get to those values, and nil them out (or you could do that in Lua directly as well) but I'm not sure why you'd want to do that. IF you're talking about Lua functions, then yes. You can push and pop and move lua functions just like any other value. If you haven't already, I suggest reading through the whole Lua manual. It's really not that long (It just took me one weekend) and it's got lots of good info. \$\endgroup\$
    – Alex Ames
    Commented Dec 16, 2011 at 0:34

Have you had a look at Luabind? See also here and here on SO.

  • \$\begingroup\$ Thanks for the advice but Luabind increase too much my project's dimensions, and it's a pain to compile under windows!! \$\endgroup\$
    – Anonymous
    Commented Dec 16, 2011 at 0:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .