LuaBridge icon indicating copy to clipboard operation
LuaBridge copied to clipboard

How to bind member function like A::get_data(double & value)

Open smartseal opened this issue 5 years ago • 4 comments

Hi, I am new to LuaBridge. I read all the docs but fail to find ways to register such function with basic type reference.

My C++ class has a member function:

class CData { public: void get_data(int index, double &val); }

I tried to use the following, addFunction("get_data", &CData::get_data) but it failed to comile with en error: luabridge\detail\typelist.h(212): error C2664: “luabridge::TypeListValues<Tail>::TypeListValues(luabridge::TypeListValues<Tail> &&)”: cannot convert parameter 1 from “double” to “Head &”

How to bind such kinds of member function?

smartseal avatar Feb 25 '20 14:02 smartseal

So you expect this should work:

  luabridge::getGlobalNamespace (L)
    .beginClass <CData> ("Int")
    .addFunction ("get_data", &CData::get_data)
    .endClass ();

... but it doesn't because there is an L-value reference parameter. LuaBridge doesn't support references and pointers for simple types (as well as Lua).

I'd suggest you using double get_data(int index) in your class.

dmitry-t avatar Mar 01 '20 08:03 dmitry-t

Is your issue solved?

dmitry-t avatar Mar 18 '20 19:03 dmitry-t

you should be able, by slighly modifying the behaviour of the scripted function like this:

luabridge::getGlobalNamespace (L)
    .beginClass <CData> ("Int")
    .addFunction ("get_data", +[](const CData* thiz, int i) { double v; thiz->get_data(i, v}; return v; })
    .endClass ();

is it feasible for you ?

kunitoki avatar Apr 17 '21 09:04 kunitoki

luabridge can't support references for trivial types because is lua that doesn't support them in the language, so you can only work it around by passing tables (tables are passed by referenca in lua) so you need to place your double in a table.

kunitoki avatar Apr 30 '22 07:04 kunitoki