title: How to store a value type in a userdata?
body: <p><a href="https://stackoverflow.com/questions/3541839/storing-values-in-a-userdata-object-from-lua">This SO article is the same thing,</a> but the answer is unhelpful because the answer was in Lua and the question was about the C-API.  So I'm asking again.  Hopefully, others will benefit from this question.</p>
  
  <p>I'm actually having 2 problems (I can't get y an z to work, and I can't get helloworld() to work)</p>
  
  <p>I'm trying to get to this:</p>
  
  <pre><code>local x = MyCBoundLib.GetSomething()
  print(x.y)
  print(x.z)
  </code></pre>
  
  <p>Where <code>x</code> is a userdata. I keep getting <code>attempt to index a userdata value</code></p>
  
  <p>I know that "<a href="https://stackoverflow.com/questions/27517499/lua-addtest-lua9-attempt-to-index-local-testobj-a-userdata-value">userdata isn't indexable without a metatable because it's C/C++ data</a>"</p>
  
  <p>In my C-code, I do something like this to try and wrap the object.</p>
  
  <pre><code>int push_Something(lua_State *L, void *object)
  {
      struct SomethingWrapper *w = (struct SomethingWrapper *)lua_newuserdata(L, sizeof(struct SomethingWrapper));
      w-&gt;object = object;
  
      luaL_setmetatable(L, "Something");
      return 1;
  }
  </code></pre>
  
  <p>Earlier, I tried to register a metatable called <code>Something</code>, like so:</p>
  
  <pre><code>luaL_newmetatable(L, "Something");
  lua_pushvalue(L, -1);
  lua_setfield(L, -2, "__index");
  luaL_setfuncs(L, some_funcs, 0);
  lua_pop(L, 1);
  </code></pre>
  
  <p>Where <code>some_funcs</code> has:</p>
  
  <pre><code>static luaL_Reg const some_funcs [] =
  {
      { "helloworld",     l_helloworld },
      { NULL, NULL }
  };
  </code></pre>
  
  <p>If I try <code>print(x.helloworld())</code>, I get the same error: <code>attempt to index a userdata value</code></p>
  
  <p>In none of my code do I know how to properly attach the value type <code>y</code> or <code>z</code>.</p>