I'm trying to learn how to use LuaBind and I've never used a scripting language before, especially not combined with C++. I'm trying to figure out how to define a function in a Lua script and then call that function from C++. I found this tutorial on the wiki which is really good, but it's obsolete and some of the function calls don't work anymore. There's also this tutorial which is more up to date, but doesn't explain how to do what I need.
Does any know of any other tutorials that explain how to do this using LuaBind, or can anyone just explain how to do it yourself? Thanks.
Calling Lua script from file
- Moohasha
- Gnoll
- Posts: 672
- Joined: Fri Dec 07, 2007 7:37 pm
- x 8
Calling Lua script from file
Black holes are where God divided by 0
- nullsquared
- Old One
- Posts: 3245
- Joined: Tue Apr 24, 2007 8:23 pm
- Location: NY, NY, USA
- x 11
Re: Calling Lua script from file
First of all, the Luabind docs do a perfect job at explaining Luabind itself: http://www.rasterbar.com/products/luabind/docs.html
As for Lua - I suggest that you first find some simple Lua tutorials (just Lua and the C interpreter, without Luabind). Once you get the basic hang of it, move onto Luabind and everything on the documentation will make sense.
As for Lua - I suggest that you first find some simple Lua tutorials (just Lua and the C interpreter, without Luabind). Once you get the basic hang of it, move onto Luabind and everything on the documentation will make sense.
- Moohasha
- Gnoll
- Posts: 672
- Joined: Fri Dec 07, 2007 7:37 pm
- x 8
Re: Calling Lua script from file
So I sort of took your advice. I read up on some Lua tutorials and learned a little bit about the language, but still couldn't figure out how to do what I was trying to do. So I ditched Lua, looked at Game Monkey and really liked it, but it has virtually no documentation/community support. Still, the tutorials for it helped me a lot with learning how to bind C++ objects to a script. So then I moved on to Squirrel (using SqPlus) and really liked that, but again, I wasn't very happy with the documentation or community. So I went back to LuaBind, and everything I had learned about scripting and binding from GameMonkey and Squirrel made LuaBind finally make sense!
All of this was an "examine your options" process and I'm still not done, but knowing more about scripting and binding I may end up with LuaBind after all, simply because it has the best documentation and support of the ones I've looked at, even though I'm still not crazy about the hulky luggage (namely boost, since I don't use it) that comes with it.
Anywho, thanks for nudging me to just look a little harder instead of handing me a bone. It really helped.
All of this was an "examine your options" process and I'm still not done, but knowing more about scripting and binding I may end up with LuaBind after all, simply because it has the best documentation and support of the ones I've looked at, even though I'm still not crazy about the hulky luggage (namely boost, since I don't use it) that comes with it.
Anywho, thanks for nudging me to just look a little harder instead of handing me a bone. It really helped.
Black holes are where God divided by 0
- nikki
- Old One
- Posts: 2730
- Joined: Sat Sep 17, 2005 10:08 am
- Location: San Francisco
- x 13
- Contact:
- Kojack
- OGRE Moderator

- Posts: 7157
- Joined: Sun Jan 25, 2004 7:35 am
- Location: Brisbane, Australia
- x 538
Re: Calling Lua script from file
To call a lua function without luabind is pretty easy:
That calls examplefunction with no parameters and no return values expected (the 0, 0 bit in the call line says 0 args, 0 returns).
If you want some params and a return value:
That one calls examplefunction("hi", 512) and gets an integer back as the return value.
Code: Select all
lua_getfield(g_state, LUA_GLOBALSINDEX, "examplefunction");
if(!lua_isnil(g_state,-1))
{
lua_call(g_state, 0,0);
}If you want some params and a return value:
Code: Select all
int retval=0;
lua_getfield(g_state, LUA_GLOBALSINDEX, "examplefunction");
if(!lua_isnil(g_state,-1))
{
lua_pushstring(g_state, "hi");
lua_pushnumber(g_state, 512);
lua_call(g_state, 2,1);
retval = lua_tointeger(g_state,-1);
}-
dudeabot
- Gnome
- Posts: 334
- Joined: Thu Jun 28, 2007 2:12 pm
- Location: Brazil
- x 5
- Contact:
Re: Calling Lua script from file
take a look on monster engine, it has extensive use of LUA and is somewhere on this forums
(it doesnt uses luabind though)
- Moohasha
- Gnoll
- Posts: 672
- Joined: Fri Dec 07, 2007 7:37 pm
- x 8
Re: Calling Lua script from file
Well I'm having mixed feelings about LuaBind, because it makes binding C++ classes/functions to Lua really stinkin' easy, but it makes the compile time go through the roof and forces you to include boost, which I wouldn't use otherwise.
Btw, what I was trying to do was execute a function in my C++ app that was defined in a .lua file. I finally figured out how to do it by calling luaL_dofile(), but even before I found that I figured I can just load the script into a string and pass it to luaL_dostring(). That executes the script, which loads the function into the global module. After that I can just call luabind::call_function() and pass it the function name as a string.
Btw, what I was trying to do was execute a function in my C++ app that was defined in a .lua file. I finally figured out how to do it by calling luaL_dofile(), but even before I found that I figured I can just load the script into a string and pass it to luaL_dostring(). That executes the script, which loads the function into the global module. After that I can just call luabind::call_function() and pass it the function name as a string.
Black holes are where God divided by 0
- nullsquared
- Old One
- Posts: 3245
- Joined: Tue Apr 24, 2007 8:23 pm
- Location: NY, NY, USA
- x 11
Re: Calling Lua script from file
That's why the Luabind documentation recommends limited use. For example, only 5 or so of your .cpp files should ever actually do Luabind stuff. Therefore, as long as you don't modify those files 24/7, compile times will not be affected as much. For functions that you use all over the place, consider creating wrappers. For example, make a quick-and-dirty wrapper of call_function, and define it in its own .cpp file. Only that .cpp file will need compiling using Luabind - all your other files that use your own wrapper will not compile slowly. Just make sure you don't include the Luabind headers in your .h file - the only thing in there should be the prototype of the function.Moohasha wrote:Well I'm having mixed feelings about LuaBind, because it makes binding C++ classes/functions to Lua really stinkin' easy, but it makes the compile time go through the roof and forces you to include boost, which I wouldn't use otherwise.
- Moohasha
- Gnoll
- Posts: 672
- Joined: Fri Dec 07, 2007 7:37 pm
- x 8
Re: Calling Lua script from file
That's actually what I'm trying to do. I have a ScriptManager than manages the Lua State and function calls, and a bindings.cpp file where all my class and function binds go. So two files which use LuaBind. This seems to work well except for one snag. I'm using a templated function in the ScriptManager to call Lua functions so that I can pass the return type I expect and the object type to pass to the script. The problem is that I just found out templates have to be defined in a header file, not a cpp. I REALLY don't want to have to expose just one part of LuaBind to everything else in the app, simply because the templates have to be in the header.
Would it work if I put the template declaration in the header and the definition in my bindings.cpp, which is the only place it would be called?
Would it work if I put the template declaration in the header and the definition in my bindings.cpp, which is the only place it would be called?
Black holes are where God divided by 0