lua-users home
lua-l archive

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


Hi.

I've been playing with Lua for the last week or so and I love it :)). I created classes for matrices and vectors as userdata from C++. A vector has x, y and z components (floats) and a matrix has right, front, up and position components as vectors. The memory layout inside the userdata matches exactly my C++ classes for matrix and vector. I override the __index and __newindex metamethods for the classes to get and set the components. Most of it works perfectly. However there is a small problem.

Lets say m is a matrix and I want to change the x coordinate of the position:

m=Matrix()
m.pos.x=10

This doesn't work because m.pos creates a new temp vector, whose x coordinate I change. In the C++ variant of the same code m.pos is a reference to the actual position inside the matrix and m.pos.x=10 works correctly. Is it possible to achieve the same result in Lua? If not, is it possible to make that code to generate an error?

Thanks
Ivo