c++ - Luabridge:返回 C++ 生命周期托管对象

标签 c++ binding lua luabridge

这个片段适用于基本类型:

int CreateBasicObject(lua_State *L)
{
    int ret0;

    lua_pushinteger(L, ret0);

    return 1;
}

在 lua 中它看起来像这样:

local NewObject=CreateBasicObject()

我将如何返回类而不是 int

push(L,&MyObject);
return 1;

似乎没有正常工作,lua部分看起来是这样的:

self.MyObject=Screen.MyObject(); 

错误是:

attempt to index field 'MyObject' (a number value)

最佳答案

newest LuaBridge您可以使用的版本RefCountedPtr例如:

一些C++定义

struct A {};

static RefCountedPtr<A> SomeA() {
 return RefCountedPtr<A>(new A);
}

和绑定(bind):

luabridge::getGlobalNamespace(L)
  .beginClass<A>("A")
   .addConstructor< void (*) (), RefCountedPtr<A> >()
  .endClass()

  .addFunction("SomeA",&SomeA);

现在你可以返回A安全地传递对象并将它们作为 RefCountedPtr<A> 传递给其他 Lua 绑定(bind)函数

在 lua 中你可以这样使用它:

local a=A()
--do something with a...

local b=SomeA()
--do something with b...

关于c++ - Luabridge:返回 C++ 生命周期托管对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16206952/

相关文章:

c++ - boost::bimap 对单射函数是否过度杀伤力?

javascript - 恩伯斯 : how to display loading spinner and notification messages on model operations

lua - 递归元方法__index?

c++ - 返回 C++ 中的映射以用作 lua 中的表

c - 删除使用 lua_newuserdata 分配的内存

一旦满足条件,C++ 就停止执行 while 循环

c++ - C++静态库中的共享全局变量

c++ - Soundflower 输入应用程序

wcf - WCF 中的大请求问题

c# - 列表中的模型绑定(bind)