lua-users home
lua-l archive

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


Ok, I've worked out what the *real* problem with implementing vectors as
simply tables is.  :-)

No, its not speed or memory! (although these need work on later)

The problem is that tables are passed by reference, meaning I get a lot of
variables all "pointing" to the same value, which I then access by element
(x,y,z, or w) affecting all those variables.  This doesn't happen with ints
or floats, because when you modify the value it *replaces* it with the new
one.

ie.

position = { x=5, y=10, z=20 }
vector = position
position.x = 10        -- vector also "changes"!!   arghhh

and no.. writing vector = vector.copy( position) or some such is *not* an
option. ;-)

What I really need is an int that has semi-table like attributes.  I don't
mind object's referencing the same copy as long as when I do vector.x = 10 I
want the whole of "vector" to be re-written with a copy.

This may not seem very flexible, but neither are ints and floats really are
they?  That's why I suggest a vector should be an atomic(?) type.

I'll start looking at the Lua source today to see how this could be done but
any hints or help would be great.

Regards

---------------------------------
Q-Games, Dylan Cuthbert.
http://www.q-games.com