[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Lua language question
- From: "Virgil Smith" <Virgil@...>
- Date: Tue, 15 Jun 2004 18:18:11 -0500
Because variables are not strongly typed in Lua (values are, but variables
aren't) typically a copy operation would be performed by creating a new
value and associating it with the target variable, rather than modifying the
value with which the variable is currently associated.
Of course this does create work for the garbage collector and if this is a
concern then you might wish to revert to modifying the associated value.
for instance
rather than...
vec = object2.vector
you might do...
function ShallowCopy(tbl, dst) do
local ret = dst or {} -- assign into table dst if provided,
local key -- otherwise make a new table
local val
repeat
key, val = next(tbl, key)
if (key) then
ret[key] = val
end
until not key
return ret
end
-- This creates a new tbl to asign into vec
vec = ShallowCopy(tbl)
-- This writes over the members of vec (but may leave extras)
ShallowCopy(tbl, vec)
-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of Dan East
Sent: Tuesday, June 15, 2004 2:58 PM
To: Lua list
Subject: Lua language question
Hi all. So far so good integrating Lua into my game engine. I'm very
impressed with the results.
I have a language specific question. Take the following C example:
Vector *pVec=&object1.vector;
*pVec=object2.vector;
So that is a slightly roundabout way of saying object1.vector =
object2.vector.
In Lua I have this:
vec=object1.vector
In my environment that is equivalent to Vector *pVec=&object1.vector. Any
changes made to vec apply to object1.vector. My question is how best
syntactically to set vec's values to that of another vector?
This will not work:
vec=object1.vector
vec2=object2.vector
vec=vec2
vec just references the same thing as vec2.
Currently I have to do this:
vec=object1.vector
vec2=object2.vector
vec.x=vec2.x
vec.y=vec2.y
vec.z=vec2.z
I could of course simply expose a function like set(), allowing something
like this:
vec.set(vec2)
However I wanted something more in the spirit of the Lua language, which
is something I'm only superficially familiar with so far (I'm more familiar
with the stack-based API side than the actual Lua language :).
Suggestions are certainly appreciated!
Dan East