I am trying to change a complex table value through a reference. It’s probably easier to explain what I am trying to achieve with code.
Say we have the table:
local list = {
["Foo"] = {
["Bar"] = true
}
}
We can change the value of Bar to false with a reference by doing:
list2 = list
list2["Foo"]["Bar"] = false
print(list["Foo"]["Bar"]) --The list's Bar value is now set to false!
I am trying to accomplish the same, but with a table that I can loop through to get to the value I want to change, like so:
local params = {"Foo", "Bar"}
list2 = list
for i = 1, #params do
list2 = list2[params[i]]
end
list2 = false
print(list["Foo"]["Bar"]) --The list's Bar value is still true!
How can I accomplish the approach taken in example #2, but keeping the reference to the original list so it changes the original list’s “Bar” value to false?
Unfortunately you cannot do that since booleans aren’t objects in Lua, and plus you can’t bind a variable to a table field so that when the variable updates the key magically will as well. Just do list2.Foo.Bar = false
Hmm, is there any way I can make it to where I can specify a list of values to go through to get to a certain end point value and change that? I am looking for an efficient method where I can change any complex table value, because sometimes the table might not be as simple as the example one posted, and some values may be located in different locations.
I am trying to change any value in a given complex table all through one function based on the parameters given, is this possible?