Reputation:
weird problem I have a program that uses multiple classes some of these classes are used to define objects inside of other class but I can't modify their values not sure if that made sense but I'll try and demonstrate
C++ file
class A{
public:
A(){
c = 0
}
int c;
};
class B{
public:
A d;
};
luabridge::getNamespace(L)
.addNamespace("test")
.addClass<A>("A")
.addConstructor<void(*) ()>()
.addData("c", &A::c)
.endClass()
.addClass<B>("B")
.addConstructor<void(*) ()>()
.addData("d", &A::d)
.endClass()
.endNamespace();
now in the lua file we have
var = test.B()
var.d.c = 2
print(var.d.c)
and the program prints
0
just to clarify if A's constructor sets c to 666 then the program outputs 666
Upvotes: 0
Views: 863
Reputation: 26
An object declared as Type variableName
is passed to Lua by value, as a copy managed by Lua. Hence var.d
returns a copy of that d
object, and with var.d.c
you are modifying the c
variable of that copy, not the c
variable of the original d
object.
An object declared as Type* variableName
is passed by reference, hence you modify the original d
object, that's why your second approach works.
More info in the LuaBridge manual.
Upvotes: 1
Reputation:
All I had to do was make the object of class A inside class B a pointer and I'm not sure why... I just found it out along the debug road after about 2 days
class A{
public:
A(){
c = 0
}
int c;
};
class B{
public:
A* d;
};
luabridge::getNamespace(L)
.addNamespace("test")
.addClass<A>("A")
.addConstructor<void(*) ()>()
.addData("c", &A::c)
.endClass()
.addClass<B>("B")
.addConstructor<void(*) ()>()
.addData("d", &A::d)
.endClass()
.endNamespace();
Upvotes: 0