ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.16k stars 504 forks source link

Static variables aren't updated. #1416

Open deadlocklogic opened 1 year ago

deadlocklogic commented 1 year ago

Lua: 5.1 Sol2: latest

C++ side:

struct MyStruct {
    static int variable;
    static int getVariable() {
        return variable;
    }
};
int MyStruct::variable = 3;

Binding:

auto _cppbind_MyStruct = state.new_usertype<::MyStruct>(
    "MyStruct",
    "getVariable", &::MyStruct::getVariable,
    "variable", sol::var(std::ref(::MyStruct::variable))
);

Lua side:

print(MyStruct.variable) -- 3
print(MyStruct.getVariable()) -- 3
MyStruct.variable = 123
print(MyStruct.variable) -- 123
print(MyStruct.getVariable()) -- 3??? expected 123

Do I need to use a property instead?

Edit: even using a property isn't calling the setter.

Smertig commented 1 year ago

Looks like a duplicate of #1268

deadlocklogic commented 1 year ago

Yes apparently, did you manage to work around this issue because its still open from 2021? Such fundamental things breaking are worrying after investing too much time with this good library. The problem is that there is virtually only one maintainer who knows all the nitpicks but apparently busy nowadays.

Smertig commented 1 year ago

I didn't find any workaround. I just replaced static properties with getter/setter, that is ugly but working solution.