Archived

This topic is now archived and is closed to further replies.

Anyone have a luabind example?

This topic is 5111 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Anyone have a quick example of using luabind? I scoured the luabind documentation and all I see is lengthy mention of modules (what the heck are those anyway??) and not much else. Nowhere could I find the normally-present, concise example of an acutal luabind script, along with some C++ class that is being bound to it. Unless of course I''m drastically missing the point and these module things ARE the point...

Share this post


Link to post
Share on other sites
Modules are similar to namespaces. Unless you need to get fairly complex I can't imagine them being really helpful.

The hard part of understanding how to use luabind is in understanding what it really is, and although the documentation is good, it has the most appalling introduction / statement of use, and it can be very hard to tell whether the code examples are lua or c++.

Luabind exports an equivalent to a C++ class, struct or function to lua.
Nothing more, nothing less.

The simplest way to get something happening is to bind a function which will accept an object that the script has generated, configuration classes are ideal to get started.

Every time I touch luabind I seem to have trouble avoiding global variables or singletons, so if anyone has avoided there use I'm looking for help as well.

On the plus side, lua and luabind do some amazing stuff behind the scenes, and I was suprised time after time that it did exactly what I wanted without me having to write extra code, especially with regards to memory management. On the down side so much templated code causescompile times to be very poor - I strongly recommend the pImpl idiom and precompiled headers.


extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
#include ".\luabind\luabind.hpp"
#include <iostream>

using namespace std;
using namespace luabind;

lua_State* L_;

class A
{
public:
A() : a_(0) {}
int get_a() const { return a_; }
void set_a(int a) { a_ = a; }
private:
int a_;
};

void sayHello(int i) {
cout << "hello " << i <<endl;
}

void retrieveData(A& a) {
cout << "retrieved " << a.get_a() << endl;
}

int main()
{
// Open Lua

L_ = lua_open();
lua_register( L_, "_ALERT", luaError );
// Prepare Luabind

luabind::open(L_);
luabind::module(L_)
[
class_<A>("A")
.def(constructor<>())
.property("a", &A::get_a, &A::set_a),
def("sayHello",&sayHello),
def("retrieveData",&retrieveData)
];
// Load required Lua libraries (optional)

lua_baselibopen(L_);
lua_iolibopen(L_);
lua_strlibopen(L_);
lua_mathlibopen(L_);
// Invoke script (error handling omitted)

lua_dofile(L_, "test.lua");
// Close Lua (Luabind shutsdown automatically)

lua_close(L_);
return 0;
}


test.lua

a = A();
a.a = 2;
sayHello(1);
retrieveData(a);
-- sayHello("foo"); -- SHOULD FAIL
-- retrieveData(1); -- SHOULD FAIL


[edited by - XXX_Andrew_XXX on December 14, 2003 8:48:46 AM]

[edited by - XXX_Andrew_XXX on December 14, 2003 8:50:13 AM]

Share this post


Link to post
Share on other sites
Thanks, this is quite helpful in my understanding of luabind! I''m still having a heck of a time getting it to link with Visual Studio- I get an unresolved external symbol every time I try to call luabind::open

Anyone else have this problem? I''ve tried playing with precompiled headers and namespaces with no luck.

Share this post


Link to post
Share on other sites
How are you linking to the luabind?

The recommended way using VC is to add *all* the luabind files to the workspace, but I found it faster and just as easy to compile luabind as a static library and then link to it - you just have to be careful about linking to the correct version (release / debug, multi-threaded / single-threaded)

Share this post


Link to post
Share on other sites