lua-users home
lua-l archive

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


> Sorry, I see now that I have not asked the right question.
> 
> I wonder "How to index a userdata, and together define methods in the
> userdata virtual table".

I just found that this method works, but it seems very complicated.

	static int index(lua_State *L)
	{
		Vect4 *v=Vget(L,1);  // Get my user data pointer
		int ch;
		if (lua_type(L,2)==LUA_TSTRING)
		{
			const char *szIndex = luaL_checkstring(L,2);
			if (!strcmp(szIndex, "normalize"))
			{
				lua_pushcfunction(L, Lnormalize);
				return 1;
			}
			if (strlen(szIndex)!=1) 
				LuaError(L, "Bad method");
			ch = *szIndex;
		}
		else if (lua_type(L,2)==LUA_TNUMBER)
		{
			ch = luaL_checkint(L,2);
		}
		else
			LuaError(L, "Bad index type");

		switch (*luaL_checkstring(L,2))
		{
		case 1: case 'x': lua_pushnumber(L,v->x); break;
		case 2: case 'y': lua_pushnumber(L,v->y); break;
		case 3: case 'z': lua_pushnumber(L,v->z); break;
		case 4: case 'w': lua_pushnumber(L,v->w); break;
		default:
			LuaError(L, "Bad index");
		}
		return 1;
	}