【问题标题】:luabridge bind C++ member, but not change member valueluabridge 绑定 C++ 成员,但不更改成员值
【发布时间】:2018-08-11 23:52:22
【问题描述】:

我有一个使用 luabridge 的问题,它更改 C++ 值失败,例如:

//c++ 文件

struct Coor3D_1 {
    int lon;
};
class ETALink{
public:
ETALink()
{

}
Coor3D_1 coor3D_1;
};

绑定代码如下:

luabridge::getGlobalNamespace(L)
.beginNamespace("test")
.beginClass<Coor3D_1>("Coor3D_1")
.addData("lon", &Coor3D_1::lon)
.endClass()

.beginClass<ETALink>("ETALink")
.addConstructor<void(*) (void)>()
.addData("coor3D_1", &ETALink::coor3D_1)
.endClass()
.endNamespace();

lua 文件如下:

eta = test.ETALink();
print("---- ", eta.coor3D_1.lon); //this is OK, I can see eta.coor3D_1.lon
eta.coor3D_1.lon = 11 //?? this is not OK, I print  eta.coor3D_1.lon is not 11

现在我的问题是为什么 eta.coor3D_1.lon = 11 不起作用? 我发现那个双“。”不会工作....

【问题讨论】:

    标签: luabridge


    【解决方案1】:

    发生这种情况是因为您的类字段成员 (coor3D_1) 作为副本传递给 Lua,因此当您更改其值时,您会更改副本,而原始对象不受影响。

    也许您可以通过以下方式解决问题:

    • 添加直接作用于 lon 字段的 ETALink 属性。
    • 将 coor3D_1 设为返回指向 Coor3D_1 结构的指针的属性。

    【讨论】:

      猜你喜欢
      • 2012-11-01
      • 2021-04-28
      • 1970-01-01
      • 2016-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-16
      • 1970-01-01
      相关资源
      最近更新 更多