lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I tried it, I still have the same problem. :(

Anders

On 4/12/06, Goran Zoricic <goran-gdalgs@croteam.com> wrote:
> I'm not a seasoned Lua veteran (yet ;) ),  but shouldn't you use:
>
> lua_setmetatable(L, -2);
>
> instead of:
>
> ...
> lua_pushliteral(L, "__metatable");
> lua_pushvalue(L, -3);
> lua_rawset(L, -3);
> ...
>
> Cheers,
>
> Goran.
>
> Anders Bergh wrote:
> > Hi!
> >
> > I'm using Lua 5.1 and I've been trying to add my own userdata type. It
> > works except I couldn't get methods working. I've been reading the
> > chapter in the first edition of pil and
> > http://lua-users.org/wiki/UserDataWithPointerExample.
> >
> > Here's the code I use:
> >
> > static const luaL_reg llua_user_f[] = {
> >  /* Functions */
> >  {"FindByNick", llua_user_FindByNick},
> >
> >  /* Methods */
> >  {"GetNick", llua_user_GetNick},
> >  {NULL, NULL}
> > };
> >
> > /* Metatable */
> > static const luaL_reg llua_user_m[] = {
> >  {"__tostring", llua_user_tostring},
> >  {NULL, NULL}
> > };
> >
> > int lluaopen_user (lua_State *L) {
> >  luaL_openlib(L, "user", llua_user_f, 0);
> >  luaL_newmetatable(L, TYPE);
> >  luaL_openlib(L, 0, llua_user_m, 0); /* mt */
> >
> >  lua_pushliteral(L, "__index");
> >  lua_pushvalue(L, -3);
> >  lua_rawset(L, -3);
> >
> >  lua_pushliteral(L, "__metatable");
> >  lua_pushvalue(L, -3);
> >  lua_rawset(L, -3);
> >
> >  lua_pop(L, 1);
> >  return 1;
> > }
> >
> > When I use "user:GetNick()" in Lua, I get the error: attempt to index
> > field '?' (a nil value).
> >
> > getmetatable(user) returns nil, tostring(user) returns "user:
> > 0x815598c". What exactly am I doing wrong?
> >
> > Anders
>
> --
> The absolute law of the universe: everything depends
>