简体   繁体   中英

luabridge bind C++ member, but not change member value

I have a question by using luabridge, it change C++ value fail, for exam:

//c++ files

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

}
Coor3D_1 coor3D_1;
};

bind code is below:

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 files is below:

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

now my question is why eta.coor3D_1.lon = 11 not work? I find that double "." will not work....

This happens because your class field member (coor3D_1) is passed to Lua as a copy, so when you change its value you change the copy, and the original object is not affected.

Probably you can solve the issue one on these ways:

  • Add an ETALink property which directly operates on the lon field.
  • Make coor3D_1 a property returning a pointer to the Coor3D_1 structure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM