سؤال

Let's consider the following C function :

void returnMultipleValuesByPointer(int* ret0, int* ret1, int* ret2)
{
    *ret0 = 0;
    *ret1 = 1;
    *ret2 = 2;
}

How to I expose it in Lua using LuaBridge ? All the documentation and examples illustrate complex cases where the pointed objects are classes but I can't find how to deal with such a simple case...

The trivial approach (ie luabridge::getGlobalNamespace(L).addFunction("test", &returnMultipleValuesByPointer)) compiles but does not understand the values are to be passed by address.

هل كانت مفيدة؟

المحلول

You can't do this directly. You have to write a wrapper method that will convert the parameters into return values. This requires writing such a method. For example, if these are pure return values (ie: they don't take parameters):

int lua_ReturnMultipleValuesByPointer(lua_State *L)
{
  int ret0;
  int ret1;
  int ret2;
  returnMultipleValuesByPointer(&ret0, &ret1, &ret2);
  lua_pushinteger(L, ret0);
  lua_pushinteger(L, ret1);
  lua_pushinteger(L, ret2);
  return 3;
}

You will need to register this with "addCFunction", since it's a standard Lua function.

نصائح أخرى

If what you want to do is something like:

a=0
b=0
c=0
test(a,b,c)
-- NOW a is 0, b is 1, c is 2

I dont think it's possible. You should revert to a lua call like this:

a,b,c=test()

Now, test can be declared this way:

luabridge::getGlobalNamespace(L).addCFunction("test", &returnMultipleValues)

int returnMultipleValues (lua_State* L) {
    push(L,0);
    push(L,1);
    push(L,2);
    return 3;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top