Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top