I got stuck trying to get my help information. One of my methods returns an instance of an object. Each time createUserInfo is called, it returns userInfoObject in lua.
However, when I call the method for the userInfo object from Lua, I cannot get the userInfo object reference ( lua_touserdata (L, 1) )
static int getUserName (lua_State *L){ UserInfo **userInfo = (UserInfo**)lua_touserdata(L,1); // The following is throwing null! Need help. // Not able to access the userInfo object. NSLog(@"UserInfo Object: %@", *userInfo); } static const luaL_reg userInstance_methods[] = { {"getUserName", getUserName}, {NULL, NULL} } int createUserInfo(lua_State *L){ UserInfo *userInfo = [[UserInfo alloc] init]; UserInfoData **userInfoData = (UserInfoData **)lua_newuserdata(L, sizeof(userInfo*)); *userInfoData = userInfo; luaL_openlib(L, "userInstance", userInstance_methods, 0); luaL_getmetatable(L, "userInfoMeta"); lua_setmetatable(L, -2); return 1; } // I have binded newUserInfo to the createUserInfo method. // I have also created the metatable for this userInfo Object in the init method. // luaL_newmetatable(L, "userInfoMeta"); // lua_pushstring(L, "__index"); // lua_pushvalue(L, -2); // lua_settable(L, -3); // luaL_register(L, NULL, userInstance_methods);
Please let me know if I am missing something!
My LuaCode fragment:
local library = require('plugin.user') local userInfo = library.newUserInfo() print(userInfo.getUserName())
Update I got rid of null after using lua_upvalueindex (1). This returns a link to an instance of user information.
UserInfo **userInfo = (UserInfo**)lua_touserdata(L,lua_upvalueindex( 1 ));
Hope this helps others too!
source share