title: How can I determine the type of lightuserdata?
body: <p>I'm adding Lua Scripting to my game engine (C++) using the Lua C API.</p>
  
  <p>I'm  pushing my objects using <code>lua_pushlightuserdata(lua_State *L, void *p)</code> and when I want to get the objects I use <code>lua_touserdata(lua_State *L, int idx)</code>, but I don't know what object I'm getting using <code>lua_touserdata()</code>.</p>
  
  <p>How can I check that?</p>
  
  <p>Here's some example code:</p>
  
  <pre><code>bool LuaScript::InitScript(const char* code, GameObject * container)
  {
     CloseLua();
  
     bool ret = false;
     luaState = LuaNewState();
     luaL_openlibs(luaState);
     RegisterAPI(luaState);
  
     /*Load the code inside .lua script and set "this" to reference the GameObject 
     containing the script and "renderer" referencing SpriteRenderer class (GameObject component)
     to show you the example.*/
     if (luaL_loadstring(luaState, code) == 0) {
         lua_pushlightuserdata(luaState, container);
         lua_setglobal(luaState, "this");
  
         SpriteRenderer* spr = new SpriteRenderer();
         lua_pushlightuserdata(luaState, spr);
         lua_setglobal(luaState, "renderer");
         ret = LuaUtils::CallFunction(luaState, NULL);
     }
     else {
         LOG_WARNING("Cannot load lua script of '%s': %s", container-&gt;name.c_str(), lua_tostring(luaState, -1));
     }
     return ret;
  }    
  
  void LuaScript::RegisterAPI(lua_State* luaState)
  {
     luaL_Reg GameObjectAPI[] = 
     {
         { "SetActive", SetGameObjectActive },
         { NULL, NULL }
     };
     LuaUtils::RegisterLibrary(luaState, GameObjectAPI, "gameObject");
  }
  
  int LuaScript::SetGameObjectActive(lua_State * luaState)
  {
     int arguments = lua_gettop(luaState);
     if (arguments != 2) {
         LOG_WARNING("SetActive(GameObject, bool) takes 2 arguments!");
     }
     else {
         if (lua_islightuserdata(luaState, 1)) {
  /*---&gt; Here it's the problem. I'm assuming that this data is a GameObject 
         but it can be other kind of data like SpriteRenderer.*/
             GameObject* go = (GameObject*)lua_touserdata(luaState, 1);
             bool active = lua_toboolean(luaState, 2);
             go-&gt;SetActive(active);
         }
     }
     return 0;
  }
  </code></pre>
  
  <p><strong>.lua Script:</strong></p>
  
  <pre class="lang-lua prettyprint-override"><code>function Start()
     gameObject.SetActive(this,false)
     gameObject.SetActive(renderer,false)
  end
  </code></pre>
  
  <p>In the above example, <code>gameObject.SetActive(this,false)</code> works because <code>this</code> is a GameObject, but <code>gameObject.SetActive(renderer,false)</code> should not work because <code>renderer</code> is not a GameObject.  But it works because I don't know a way to check if it's a GameObject or SpriteRenderer and the variable <code>go</code> at line <code>GameObject* go = (GameObject*)lua_touserdata(luaState, 1);</code> inside <code>LuaScript::SetGameObjectActive(lua_State * luaState)</code> is malformed (nullptr's, memory errors, etc.) because I'm assigning the data as a GameObject.</p>