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".

Charles

> -----Message d'origine-----
> De : lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] De
> la part de Charles Melice
> Envoyé : lundi 5 janvier 2015 21:36
> À : 'Lua mailing list'
> Objet : Two indexing methods of a vector. How to ?
> 
> I defined (with the C API) a Vector object based on full user data.
> 
> 	static Vec4 *Vget(lua_State *L, int i)
> 	{
> 		if (luaL_checkudata(L,i,MYTYPE)==NULL) ERROR(MYTYPE);
> 		return (Vec4*)lua_touserdata(L,i);
> 	}
> 
> Now, I'm interested to be able to index the 'x, y, z, t' coordinates with
> numeric indexes (1,2,3,4). For instance :
> 
> 	local v = Vec4(11,22,33,44)
> 	print(v.y) -- 22
> 	v:normalize()
> 	print(v) -- Vec4(0.27, 0.53, 0.80, 1.07)
> 	print(v[2]) -- 0.53 ???
> 
> 	v[2]=22.22 -- ???
> 	print(v.y) -- 22.22
> 
> I don't see how to do that ?
> 
> Thanks,
> Charles