1
General discussions / Lua and C++
« on: January 29, 2011, 11:40:29 am »
Hi!
Lua binding is something I just done recently in my code, I too searched for the best way to easily make a connection between Lua and C++.
I heard of LuaBind like Walker, but its syntax did not suit me well when using it. What I would recommend you is to use Lunar, which is an updated version of the Luna binding, for the last version of Lua, 5.1.
Lunar is really easy to use and it really lets you keep your Object oriented programming style when binding. One cool thing is that it also allow you to choose if a C++ class used in Lua code as an instance must be destructed automatically by the Lua GarbageCollector or if you (the C++ programmer), are already taking care of the destruction of this instance. This is a really useful feature. Lunar is just a template class in a unique .hpp file, so really easy to integrate too.
Here is the link with some samples :
http://lua-users.org/wiki/CppBindingWithLunar
To improve Lunar/Lua ease of use in my code I just created two more classes, one is "LuaValue" which is an object who can take any Lua value (boolean, number, string or table) in it to store a Lua parameter(s) or return value(s) really easily for the programmer to use them in the c++ code. And I did another class (named "Lua"), as a Singleton, which store the lua_State* and offer some functions too, for example :
- Loading Lua scripts
- Calling Lua functions with parameters (and return values) from C++
- Registering C++ Function to be called later in a Lua script
- Retrieving table elements stored in a Lua table, from the C++ code (Lua Table can be considered as "objects", so its like retrieving a Lua object attribute from C++)
- Retrieving global variables
- etc...
If you want some advice or code or help with all that, I will be glad to do so !
Lua binding is something I just done recently in my code, I too searched for the best way to easily make a connection between Lua and C++.
I heard of LuaBind like Walker, but its syntax did not suit me well when using it. What I would recommend you is to use Lunar, which is an updated version of the Luna binding, for the last version of Lua, 5.1.
Lunar is really easy to use and it really lets you keep your Object oriented programming style when binding. One cool thing is that it also allow you to choose if a C++ class used in Lua code as an instance must be destructed automatically by the Lua GarbageCollector or if you (the C++ programmer), are already taking care of the destruction of this instance. This is a really useful feature. Lunar is just a template class in a unique .hpp file, so really easy to integrate too.
Here is the link with some samples :
http://lua-users.org/wiki/CppBindingWithLunar
To improve Lunar/Lua ease of use in my code I just created two more classes, one is "LuaValue" which is an object who can take any Lua value (boolean, number, string or table) in it to store a Lua parameter(s) or return value(s) really easily for the programmer to use them in the c++ code. And I did another class (named "Lua"), as a Singleton, which store the lua_State* and offer some functions too, for example :
- Loading Lua scripts
- Calling Lua functions with parameters (and return values) from C++
- Registering C++ Function to be called later in a Lua script
- Retrieving table elements stored in a Lua table, from the C++ code (Lua Table can be considered as "objects", so its like retrieving a Lua object attribute from C++)
- Retrieving global variables
- etc...
If you want some advice or code or help with all that, I will be glad to do so !