LuaBridge icon indicating copy to clipboard operation
LuaBridge copied to clipboard

static const class member support?

Open spartawhy117 opened this issue 2 years ago • 2 comments

current use addStaticProperty will get error, so how to bind class static const member to Lua?

spartawhy117 avatar Sep 14 '22 07:09 spartawhy117

Indeed, such function is missing. addStaticProperty() either requires a non-const member or both getter and setter. As a workaround you can currently register a setter that does nothing or throws. E.g. assuming const MyConstStaticDataType MyClass:constStaticData:

luabridge::getGlobalNamespace(L)
    .beginClass<MyClass>("MyClass")
    .addStaticProperty(
        "staticProperty",
        +[] { return MyClass::constStaticData; },
        +[](MyConstStaticDataType) { /*or throw here*/ });

dmitry-t avatar Sep 15 '22 22:09 dmitry-t

Indeed, such function is missing. addStaticProperty() either requires a non-const member or both getter and setter. As a workaround you can currently register a setter that does nothing or throws. E.g. assuming const MyConstStaticDataType MyClass:constStaticData:

luabridge::getGlobalNamespace(L)
    .beginClass<MyClass>("MyClass")
    .addStaticProperty(
        "staticProperty",
        +[] { return MyClass::constStaticData; },
        +[](MyConstStaticDataType) { /*or throw here*/ });

if someone write in Lua like this

      local constData = MyClass.constStaticData
      constData = new value

     ... other code
     local dir =MyClass.constStaticData * other value

the first one want to modify the const value will get error? and the second one use that var is still default value in c#?

spartawhy117 avatar Oct 06 '22 07:10 spartawhy117