Question

I just downloaded LuaBridge today and am very satisfied with it so far. One thing I've noticed is that I'm able to circumvent the normal requirement of having a lua_State as a function parameter.

I can do this:

//C++ files
void love(int i) {std::cout << i;}

luabridge::getGlobalNamespace(lua)
  .addFunction("love", love);

-- Lua file
love(8)

and it will work just fine, but if I do anything to the effect of:

//C++ files
struct Tester {
  int number;
  void MemFunction (int i) { std::cout << i;}
  static void Register(lua_State*); 
};

void Tester::Register(lua_State *lua) {
  luabridge::getGlobalNamespace(lua)
    .beginClass<Tester>("Tester")
    .addConstructor <void (*) (void)> ()
    .addData("number", &Tester::number)
    .addFunction("MemFunction", &Tester::MemFunction)
 .endClass();
}

--Lua file
  c = Tester()      -- works...
  c.number = 1      -- works...
  c.MemFunction(10) -- nothing!

Nothing I've read in the documentation indicates that member functions with non-lua_State arguments can't be registered, and I've seen some LuaBridge code doing just this with no problem. What am I doing wrong here?

Was it helpful?

Solution

You have to use the method call syntax

c:MemFunction(10)

I'd suggest you use the newer version from github, which has an extensive documentation. It also allows some extra flexibility concerning the input parameters and return values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top