Hi there,
I'm confused... Whenever I assign a metatable to a light userdata, ALL other, already metatabled lightuserdatas are asigned to the same metatable. If I'm doing the same with tables, every table gets its unique metatable. Please give my example code a try. It basicly creates 3 new userdata and 2 tables and give them unique metatables... but somehow, as said, all light userdatas change their metatable when a single one gets its.
#include <stdio.h>
#ifdef __cplusplus
extern "C"
{
#endif
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#ifdef __cplusplus
}
#endif
int main(int argc, char** argv)
{
char str[512];
lua_State *L = lua_open();
luaL_openlibs(L);
lua_pushlightuserdata(L, (void*)0xff00ff00);
lua_setglobal(L, "one");
lua_getglobal(L, "one");
luaL_newmetatable(L, "_one");
lua_setglobal(L, "_one");
lua_getglobal(L, "_one");
lua_setmetatable(L, -2);
sprintf(str, "print(\"1 Metatable of one: \"..tostring(getmetatable(one)))"); luaL_dostring (L, str);
sprintf(str, "print(\"1 Metatable of two: \"..tostring(getmetatable(two)))"); luaL_dostring (L, str);
sprintf(str, "print(\"1 Metatable of three: \"..tostring(getmetatable(three)))"); luaL_dostring (L, str);
sprintf(str, "print(\"1 Metatable of four: \"..tostring(getmetatable(four)))"); luaL_dostring (L, str);
sprintf(str, "print(\"1 Metatable of five: \"..tostring(getmetatable(five)))"); luaL_dostring (L, str);
printf("\n");
lua_pushlightuserdata(L, (void*)0xcc00cc00);
lua_setglobal(L, "two");
lua_getglobal(L, "two");
lua_newtable(L);
lua_setglobal(L, "_two");
lua_getglobal(L, "_two");
lua_setmetatable(L, -2);
sprintf(str, "print(\"2 Metatable of one: \"..tostring(getmetatable(one)))"); luaL_dostring (L, str);
sprintf(str, "print(\"2 Metatable of two: \"..tostring(getmetatable(two)))"); luaL_dostring (L, str);
sprintf(str, "print(\"2 Metatable of three: \"..tostring(getmetatable(three)))"); luaL_dostring (L, str);
sprintf(str, "print(\"2 Metatable of four: \"..tostring(getmetatable(four)))"); luaL_dostring (L, str);
sprintf(str, "print(\"2 Metatable of five: \"..tostring(getmetatable(five)))"); luaL_dostring (L, str);
printf("\n");
lua_newtable(L);
lua_setglobal(L, "four");
lua_getglobal(L, "four");
lua_newtable(L);
lua_setglobal(L, "_four");
lua_getglobal(L, "_four");
lua_setmetatable(L, -2);
sprintf(str, "print(\"4 Metatable of one: \"..tostring(getmetatable(one)))"); luaL_dostring (L, str);
sprintf(str, "print(\"4 Metatable of two: \"..tostring(getmetatable(two)))"); luaL_dostring (L, str);
sprintf(str, "print(\"4 Metatable of three: \"..tostring(getmetatable(three)))"); luaL_dostring (L, str);
sprintf(str, "print(\"4 Metatable of four: \"..tostring(getmetatable(four)))"); luaL_dostring (L, str);
sprintf(str, "print(\"4 Metatable of five: \"..tostring(getmetatable(five)))"); luaL_dostring (L, str);
printf("\n");
lua_pushlightuserdata(L, (void*)0x88008800);
lua_setglobal(L, "three");
lua_getglobal(L, "three");
lua_newtable(L);
lua_setglobal(L, "_three");
lua_getglobal(L, "_three");
lua_setmetatable(L, -2);
sprintf(str, "print(\"3 Metatable of one: \"..tostring(getmetatable(one)))"); luaL_dostring (L, str);
sprintf(str, "print(\"3 Metatable of two: \"..tostring(getmetatable(two)))"); luaL_dostring (L, str);
sprintf(str, "print(\"3 Metatable of three: \"..tostring(getmetatable(three)))"); luaL_dostring (L, str);
sprintf(str, "print(\"3 Metatable of four: \"..tostring(getmetatable(four)))"); luaL_dostring (L, str);
sprintf(str, "print(\"3 Metatable of five: \"..tostring(getmetatable(five)))"); luaL_dostring (L, str);
printf("\n");
lua_newtable(L);
lua_setglobal(L, "five");
lua_getglobal(L, "five");
lua_newtable(L);
lua_setglobal(L, "_five");
lua_getglobal(L, "_five");
lua_setmetatable(L, -2);
sprintf(str, "print(\"5 Metatable of one: \"..tostring(getmetatable(one)))"); luaL_dostring (L, str);
sprintf(str, "print(\"5 Metatable of two: \"..tostring(getmetatable(two)))"); luaL_dostring (L, str);
sprintf(str, "print(\"5 Metatable of three: \"..tostring(getmetatable(three)))"); luaL_dostring (L, str);
sprintf(str, "print(\"5 Metatable of four: \"..tostring(getmetatable(four)))"); luaL_dostring (L, str);
sprintf(str, "print(\"5 Metatable of five: \"..tostring(getmetatable(five)))"); luaL_dostring (L, str);
printf("\n");
return 0;
}
[Lua] Multiple Light Userdata with Metatables
Every light userdata shares the same metatable, if you want to assign different metatables you either need to use tables or full user data.
edit added Lua manual quote
http://www.lua.org/manual/5.1/manual.html#2.8
edit added Lua manual quote
http://www.lua.org/manual/5.1/manual.html#2.8
Quote:Tables and full userdata have individual metatables (although multiple tables and userdata can share their metatables). Values of all other types share one single metatable per type; that is, there is one single metatable for all numbers, one for all strings, etc.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement