A very quick question about Lua and variable reassignment

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
dizzykiwi3
Citizen
Posts: 58
Joined: Tue Jan 14, 2014 2:03 am

A very quick question about Lua and variable reassignment

Post by dizzykiwi3 »

This may be a very simple question, but I'm just curious

say I have a function setit

Code: Select all

function setit(x)
x = 5 end
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?
User avatar
markgo
Party member
Posts: 190
Joined: Sat Jan 05, 2013 12:21 am
Location: USA

Re: A very quick question about Lua and variable reassignmen

Post by markgo »

It's basically the same as this

Code: Select all

bob = 10
function setit()
  local x = bob
  x = 5
end
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: A very quick question about Lua and variable reassignmen

Post 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 :awesome:
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
dizzykiwi3
Citizen
Posts: 58
Joined: Tue Jan 14, 2014 2:03 am

Re: A very quick question about Lua and variable reassignmen

Post by dizzykiwi3 »

That was outrageously clear and concise Hugo, thanks a bunch!
User avatar
dizzykiwi3
Citizen
Posts: 58
Joined: Tue Jan 14, 2014 2:03 am

Re: A very quick question about Lua and variable reassignmen

Post 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
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: A very quick question about Lua and variable reassignmen

Post by HugoBDesigner »

Glad I could help :ultraglee:
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 :D


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
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: A very quick question about Lua and variable reassignmen

Post 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
Help us help you: attach a .love.
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: A very quick question about Lua and variable reassignmen

Post by HugoBDesigner »

Welp, it turns out I shouldn't have left the Lua school early :rofl:
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 :awesome:
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
dizzykiwi3
Citizen
Posts: 58
Joined: Tue Jan 14, 2014 2:03 am

Re: A very quick question about Lua and variable reassignmen

Post by dizzykiwi3 »

So... what should I do to accomplish what I need?
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: A very quick question about Lua and variable reassignmen

Post by HugoBDesigner »

This will still work:

Code: Select all

variable1 = 20

function setit(x)
_G[x] = 10
end

setit("variable1") --Now variable1 is 10!
@HugoBDesigner - Twitter
HugoBDesigner - Blog
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest