lua-users home
lua-l archive

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


> I have added a couple of 3D object types (vector, matrix, 
> quaternion) to Lua as userdata objects. The default behavior of a 
> statement like:
> 
> 	vector2 = vector1
> 
> is to set both values to reference the same userdata object. But I 
> would like my math objects to behave like the simple data types, in 
> that assignment makes a *copy* of the object, and changing vector2 
> would have no effect on vector1. Is there an easy way to accomplish 
> this in Lua? 

I'm not sure this is a good idea as you may be ambiguities, or it wont be
clear whats happening in the code. It may also be pretty inefficient. I dont
think you'll *always* want to copy a vector? Perhaps you should use a copy
constructor to explicitly state whats going on?

eg.
       vector1 = vector()  -- constructor
       vector1 = vector{1,2,3}  -- constructor
       vector2 = vector(vector1)  -- copy constructor
       vector3 = vector1  -- reference

Where you use operators you can create new vectors:
eg.   vector1 = vector2 + vector3


Regards,
Nick