Posted 25 June 2013 - 11:31 PM
Can functions set variables to be used later, and if so how?
local someVar = 1 --# your variable for later
local function change( num )
someVar = num
end
print(someVar)
change(5)
print(someVar)
change(7)
print(someVar)
local someVar = 1
local function change( someVar )
someVar = someVar
end
print(someVar)
change(5)
print(someVar)
change(7)
print(someVar)
this will always output 1.
function change(var1, toV)
_G[var1] = _G[toV];
end
The funny part is, that it doesnt work like that. If you store something globally, so not with the local prefix, it still wont work. Sinc _G is a table and version can be anything.If you need to change a specific variable in a function you can usefunction change(var1, toV) _G[var1] = _G[toV]; end
If you call change(version, "TestProgram") it would be the same as version = "TestProgram"
I used this for setting variables a while ago.
version = 1.5
local function change( x, z )
_G[x] = _G[z]
end
change( version, "newv" ) -- Now the function runs like this: _G[1.5] = _G["newv"]
local function change( var, toVar ) -- Both needs to be stringed
_G[toVar] = _G[var]
_G[var] = nil
end
--SImpler:
local function change( var, toVar ) -- int - stringed
_G[toVar] = var
-- Cannot set var to nil
end
And:Yes they can, just define the variable at the top of your code.local someVar = 1 --# your variable for later local function change( num ) someVar = num end print(someVar) change(5) print(someVar) change(7) print(someVar)
Also do note, that there are some cases where it will not be able to set the variable, and that is when your name any variables in your function the same as ones outside, in this case when out use the variable it will always be the one inside the function and not modify the external one. examplethis will always output 1.local someVar = 1 local function change( someVar ) someVar = someVar end print(someVar) change(5) print(someVar) change(7) print(someVar)
function generate()
local var = 0
local function change(new)
var = new
end
local function get()
return var
end
return change, get
end
f1, f2 = generate()
If you run it, Lua will create a variable without global name, you can only access it via 2 functions returned by generate(), they'll have global names "f1" and "f2".That could work, but its a lot more cumbersome than what I've posted and requires knowledge of function pointers, plus its just means lots of functions, if you're going to take advantage of pointers just use a table.And:If you run it, Lua will create a variable without global name, you can only access it via 2 functions returned by generate(), they'll have global names "f1" and "f2".function generate() local var = 0 local function change(new) var = new end local function get() return var end return change, get end f1, f2 = generate()
f1(new_value) will modify it, f2() will return it. You can also store f1 as local, then you'll have a variable that everyone can get, but only you can change.
local vars = {}
local function change(tbl, idx, new)
tbl[idx] = new
end
vars["somevar"] = 5