Page 1 of 2
A very quick question about Lua and variable reassignment
Posted: Fri Feb 06, 2015 6:37 pm
by dizzykiwi3
This may be a very simple question, but I'm just curious
say I have a function setit
if I define a variable bob to be 10 and pass it through setit, bob will still be equal to 10. Why is this the case?
Re: A very quick question about Lua and variable reassignmen
Posted: Fri Feb 06, 2015 6:43 pm
by markgo
It's basically the same as this
Code: Select all
bob = 10
function setit()
local x = bob
x = 5
end
Re: A very quick question about Lua and variable reassignmen
Posted: Fri Feb 06, 2015 6:46 pm
by HugoBDesigner
The problem is that, the way you're doing it, you can't detect which variable you're trying to change. For example:
Code: Select all
variable1 = 12
variable2 = "something"
function setit(x)
x = 10
end
When you call it with the variable "variable1", it's gonna send only the variable value, not it's name, so it'd be like:
Code: Select all
setit(variable1)
function setit(local x = 12) --notice how it sets a new variable instead of detecting the one used
x = 10 --now the last "x" (which is a local variable) is the only thing changed
end --After you end the function, the local variable is cleaned, so no change whatsoever has been made in any variable
A nice thing about Lua, though, is that setting table values DOES change the original value, so for instance:
Code: Select all
variable1 = {["a"] = 12, ["b"] = 5}
function setit(x)
x = 10
end
setit(variable1.a) --variable1.a is now 10
Luckily for us, variables are stored in a table called _G, so you can still change a variable's specific value, but you'll have to send the variable's name as a string instead:
Code: Select all
variable1 = 20
function setit(x)
_G[x] = 10
end
setit("variable1") --Now variable1 is 10!
I hope I could make this clear, and I hope I helped too
Re: A very quick question about Lua and variable reassignmen
Posted: Fri Feb 06, 2015 6:49 pm
by dizzykiwi3
That was outrageously clear and concise Hugo, thanks a bunch!
Re: A very quick question about Lua and variable reassignmen
Posted: Fri Feb 06, 2015 6:54 pm
by dizzykiwi3
The code for the table values though doesn't seem to be working for me
Code: Select all
variable1 = {["a"] = 12, ["b"] = 5}
function setit(x)
x = 10
end
setit(variable1.a) --variable1.a is now 10
when I copy and paste that into my local console, variable1.a still returns 12
Re: A very quick question about Lua and variable reassignmen
Posted: Fri Feb 06, 2015 7:02 pm
by HugoBDesigner
Glad I could help
One thing to add, though: This function can be obsolete in most cases, since you can simply change the variable you want to the new value:
Code: Select all
variable1 = 12
--later in the code
variable1 = 10
But this is mostly useful if you need to change multiple variables at once:
Code: Select all
local toChange = {"variable1", "variable2", "variable3", "variable4", ... }
for i, v in ipairs(toChange) do
setit(v)
end
Other than this case, I don't see many needs for that. Since the function has only a single line of code, you could replace any call of it with it's code too:
Code: Select all
local toChange = {"variable1", "variable2", "variable3", "variable4", ... }
for i, v in ipairs(toChange) do
_G[v] = 10
end
But that doesn't mean that what you're trying to do won't work. Just use whatever you feel comfortable with, I just thought I could let this post here, in case you feel like you could use something else
EDIT:
dizzykiwi3 wrote:The code for the table values though doesn't seem to be working for me
Code: Select all
variable1 = {["a"] = 12, ["b"] = 5}
function setit(x)
x = 10
end
setit(variable1.a) --variable1.a is now 10
when I copy and paste that into my local console, variable1.a still returns 12
Oh, I'm sorry. Forgot that you actually need to specify the table. So this:
Code: Select all
variable1 = {["a"] = 12, ["b"] = 5}
function setit(x)
for i, v in pairs(x) do
v = 10
end
end
setit(variable1)
Is what would change the values. In this specific example, it changes all the values in the table, but you could also make it change only a single value, or reassemble the entire table, if you REALLY need to:
Code: Select all
variable1 = {12} --variable1[1] is 12
function setit(x)
x = {10}
end
setit(variable1) --variable1[1] is now 10
Re: A very quick question about Lua and variable reassignmen
Posted: Fri Feb 06, 2015 11:44 pm
by Robin
HugoBDesigner wrote:Oh, I'm sorry. Forgot that you actually need to specify the table. So this:
Code: Select all
variable1 = {["a"] = 12, ["b"] = 5}
function setit(x)
for i, v in pairs(x) do
v = 10
end
end
setit(variable1)
Is what would change the values. In this specific example, it changes all the values in the table, but you could also make it change only a single value, or reassemble the entire table, if you REALLY need to:
Code: Select all
variable1 = {12} --variable1[1] is 12
function setit(x)
x = {10}
end
setit(variable1) --variable1[1] is now 10
Wrong and wrong. (I'm sorry, you were being so helpful to dizzykiwi3)
In Lua, = is "bind", not "assign". If you do
avar = anexpression, the only thing that changes is where
avar points to. No table is modified, even if it previously pointed to such a table (or a value pointed to by some table).
If
a.b is a table, and you do
a.b = {}, then only the table
a is changed. The Table Previously Known As
a.b is unaffected.
You can try it out:
Code: Select all
a = {b = {10}}
local original_table = a.b
print(a.b[1]) -- 10
a.b = {5)
print(a.b[1]) -- 5
print(original_table[1]) -- 10
Re: A very quick question about Lua and variable reassignmen
Posted: Sat Feb 07, 2015 2:32 am
by HugoBDesigner
Welp, it turns out I shouldn't have left the Lua school early
Seriously, though, I thought I had these table-persistence things understood, but I guess not. Thanks for the correction, though. I'll read a bit more about Lua tables and variables, just so I get these things properly understood
Re: A very quick question about Lua and variable reassignmen
Posted: Sat Feb 07, 2015 6:30 am
by dizzykiwi3
So... what should I do to accomplish what I need?
Re: A very quick question about Lua and variable reassignmen
Posted: Sat Feb 07, 2015 7:49 am
by HugoBDesigner
This will still work:
Code: Select all
variable1 = 20
function setit(x)
_G[x] = 10
end
setit("variable1") --Now variable1 is 10!