lua-users home
lua-l archive

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



> From: Dylan Cuthbert 
> 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. ;-)


Yes, they're references. I take your point that vectors are pretty much
CPU integral types now but this is an issue about general assignment of
Lua objects - which for everything but numbers are references. Don't
other interpreted languages behave in the same way (e.g. Python, Java,
Perl?), i.e. everything is a reference. It wasn't so long ago you
wouldn't have dreamed of passing vectors around by value.

Isn't this just about semantics?

	position = { x=5, y=10, z=20 }
	vector.copies(position)
	position.x = 10        -- position is new vector

or

	position = { x=5, y=10, z=20 }
	vector = Vector(position)  -- constructed
	position.x = 10        -- position is new vector

What happens when you don't want to make a new vector and you want a
reference?


Regards,
Nick