This is my first attempt with LUA language, and I'm a little bit confused with variables declaration. I need to declare all variables as local, and as far I understand, local variable is only active on its scope (e.g. the function). What I need is pass a specific value to a function, "elaborate" it, and then assign another local function to be elaborated later, let's make a simple example:
local function test(x)
local z=x
return z
end
local x=1
test(x)
print(z)
So, 'x' is declared at main as local, then passed to the function. That function will just elaborate it and then assign the value to another local variable 'z'. The new variable will be used later outside the function. In the example I only assigned values, in the final code there will be much more operation on variables.
However the above print(z) will result in a nil value. I know that I can declare z as global, but unfortunately I have to keep local all variables because the script will be called by several thread, and 'x' can have different values while 'z' will be written as output.
Could someone help me to understand how to do that? Thank you, Lucas
EDIT: I can do something like:
local function test(x, y)
local z=x+y
local w=y-x
return z,w
end
local z,w = test(1,2)
print(z)
print(w)
Is it a good approach? ...
return. Check out:do local function test(x, y) return x + y, y - x end print(test(1, 2)) end