lua-users home
lua-l archive

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


On Tue, Aug 18, 2009 at 02:39:32AM +0100, Chris Camacho wrote:
>
>> You can use lua_pushvalue to duplicate the reference to the userdata
>> on the stack. Then you can save one in the weak table (with the light
>> userdata key), and do whatever you want with the other one.
>>   
>  char buf[40];
>  cpBody *bb = (cpBody *)lua_newuserdata(L, sizeof(cpBody));
>  lua_pushvalue(L,-1);
>  lua_pushstring(L,"__cpBody_ptrs");
>  sprintf(buf,"%ld\0",(long)bb);
>  lua_setfield(L,-2,buf);
>
>  luaL_getmetatable(L, "cpBody");
>  lua_setmetatable(L, -2);
>  return bb;
>
> what on earth does attempt to index a userdata value mean?
>
> Thanks
> Chris

foo = some_function_that_creates_a_userdata()
print(foo.bar)

It means you tried to index a userdata, in your example you tried to
setfield on a userdata, you can't do that by default. In order for that to
work the userdata needs to have an __index metamethod.

    -Etan