say I have a function setit
Code: Select all
function setit(x)
x = 5 end
Code: Select all
function setit(x)
x = 5 end
Code: Select all
bob = 10
function setit()
local x = bob
x = 5
end
Code: Select all
variable1 = 12
variable2 = "something"
function setit(x)
x = 10
end
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
Code: Select all
variable1 = {["a"] = 12, ["b"] = 5}
function setit(x)
x = 10
end
setit(variable1.a) --variable1.a is now 10
Code: Select all
variable1 = 20
function setit(x)
_G[x] = 10
end
setit("variable1") --Now variable1 is 10!
Code: Select all
variable1 = {["a"] = 12, ["b"] = 5}
function setit(x)
x = 10
end
setit(variable1.a) --variable1.a is now 10
Code: Select all
variable1 = 12
--later in the code
variable1 = 10
Code: Select all
local toChange = {"variable1", "variable2", "variable3", "variable4", ... }
for i, v in ipairs(toChange) do
setit(v)
end
Code: Select all
local toChange = {"variable1", "variable2", "variable3", "variable4", ... }
for i, v in ipairs(toChange) do
_G[v] = 10
end
Oh, I'm sorry. Forgot that you actually need to specify the table. So this:dizzykiwi3 wrote:The code for the table values though doesn't seem to be working for me
when I copy and paste that into my local console, variable1.a still returns 12Code: Select all
variable1 = {["a"] = 12, ["b"] = 5} function setit(x) x = 10 end setit(variable1.a) --variable1.a is now 10
Code: Select all
variable1 = {["a"] = 12, ["b"] = 5}
function setit(x)
for i, v in pairs(x) do
v = 10
end
end
setit(variable1)
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)HugoBDesigner wrote:Oh, I'm sorry. Forgot that you actually need to specify the table. So this: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 = {["a"] = 12, ["b"] = 5} function setit(x) for i, v in pairs(x) do v = 10 end end setit(variable1)
Code: Select all
variable1 = {12} --variable1[1] is 12 function setit(x) x = {10} end setit(variable1) --variable1[1] is now 10
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
Code: Select all
variable1 = 20
function setit(x)
_G[x] = 10
end
setit("variable1") --Now variable1 is 10!
Users browsing this forum: No registered users and 1 guest