title: Lua: methods and properties when exporting class from c
body: <p>I use lua as a scripting language for my 3d engine. I have lua "classes" for several objects and now I want to use properties instead of getters and setters. So instead of something like this</p>
  
  <pre class="lang-lua prettyprint-override"><code>local oldState = ui:GetChild("Panel1"):GetVisible()
  ui:GetChild("Panel1"):SetVisible(not oldState)
  </code></pre>
  
  <p>I would just</p>
  
  <pre class="lang-lua prettyprint-override"><code>ui.Panel1.visible = not ui.Panel1.visible
  </code></pre>
  
  <p>The problem is my C++ code for creating metatables and instanced overrides the __index method. Here it is by the way:</p>
  
  <ol>
  <li><p>Create a metatable:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>void CLUAScript::RegisterClass(const luaL_Reg funcs[], std::string const&amp; className)
  {
      luaL_newmetatable(m_lua_state, std::string("Classes." + className).c_str());
      luaL_newlib( m_lua_state, funcs);
      lua_setglobal(m_lua_state, className.c_str());
  }
  </code></pre></li>
  <li><p>Instantiate the class (the lua object only holds a pointer to an actual data that is stored in C++ code):</p>
  
  <pre class="lang-cpp prettyprint-override"><code>int CLUAScript::NewInstanceClass(void* instance, std::string const&amp; className)
  {
      if (!instance)
      {
          lua_pushnil(m_lua_state);
          return 1;
      }
  
      luaL_checktype(m_lua_state, 1, LUA_TTABLE);
  
      lua_newtable(m_lua_state);
  
      lua_pushvalue(m_lua_state,1);       
      lua_setmetatable(m_lua_state, -2);
  
      lua_pushvalue(m_lua_state,1);
      lua_setfield(m_lua_state, 1, "__index");  
  
      void **s = (void **)lua_newuserdata(m_lua_state, sizeof(void *));
  
      *s = instance;
      luaL_getmetatable(m_lua_state, std::string("Classes." + className).c_str());
      lua_setmetatable(m_lua_state, -2);
      lua_setfield(m_lua_state, -2, "__self"); 
  
      return 1; 
  }
  </code></pre></li>
  </ol>
  
  <p>The question is how can I have both methods and properties. If I just add <code>__index</code> to <code>CLUAScript::RegisterClass</code> funcs array it is never called. And I cannot imagine a way to remove its redefinition in <code>CLUAScript::NewInstanceClass</code>.</p>
  
  <p>If this code is not enough, here is the links to files working with lua:
  <a href="https://code.google.com/p/wargameengine/source/browse/trunk/WargameEngine/WargameEngine/WargameEngine/controller/LUAScriptHandler.cpp" rel="nofollow">lua helper class</a>, 
  <a href="https://code.google.com/p/wargameengine/source/browse/trunk/WargameEngine/WargameEngine/WargameEngine/controller/LUARegisterUI.cpp" rel="nofollow">functions for UI</a>, 
  <a href="https://code.google.com/p/wargameengine/source/browse/trunk/WargameEngine/WargameEngine/WargameEngine/controller/LUARegisterObject.cpp" rel="nofollow">functions for Objects</a>, and
  <a href="https://code.google.com/p/wargameengine/source/browse/trunk/WargameEngine/WargameEngine/WargameEngine/Killteam/test.lua" rel="nofollow">testing lua script</a></p>