Can functions set variables to be used later, and if so how?
Functions Question
Started by johnnic, Jun 25 2013 10:31 PM
7 replies to this topic
#1
Posted 25 June 2013 - 10:31 PM
#2
Posted 25 June 2013 - 10:36 PM
Yes they can, just define the variable at the top of your code.
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. example
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. example
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.
#3
Posted 25 June 2013 - 11:01 PM
Thanks. I am trying to make my API and was trying to implement a buttons option.
#4
Posted 27 June 2013 - 07:42 AM
If you need to change a specific variable in a function you can use
If you call change(version, "TestProgram") it would be the same as version = "TestProgram"
I used this for setting variables a while ago.
function 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.
#5
Posted 27 June 2013 - 09:34 AM
Imque, on 27 June 2013 - 07:42 AM, said:
If you need to change a specific variable in a function you can use
If you call change(version, "TestProgram") it would be the same as version = "TestProgram"
I used this for setting variables a while ago.
function 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.
Lets assume version 1.5: (Im doing what there is code wise )
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"]
So it would only work with strings, and btw, you need to change it around:
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
Your concept was good though
#6
Posted 28 June 2013 - 09:23 AM
Oh right. I see what I did wrong. Thanks man.
#7
Posted 30 June 2013 - 03:23 PM
theoriginalbit, on 25 June 2013 - 10:36 PM, said:
Yes they can, just define the variable at the top of your code.
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. example
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. example
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 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".
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.
#8
Posted 01 July 2013 - 05:51 AM
0099, on 30 June 2013 - 03:23 PM, said:
And:
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.
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".
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
Edited by theoriginalbit, 01 July 2013 - 05:53 AM.
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users











