有没有办法将表指向另一个表?例如:
local a = {}
local b = {}
a.name = "Josh"
print(a.name) -- Prints 'Josh'
print(b.name) -- Prints 'Josh' aswell
a.name = "I don't have a name"
print(a.name) -- Print 'I don't have a name'
print(b.name) -- Prints 'I don't have a name' aswell
我希望你明白我的意思..谢谢
编辑:
好的,这就是这个想法:
我正在制作一个像这样的动态功能
local table = { 1, 2, "hey" }
function drawimage(name, posx, posy referencetable)
_tabledata[name] = { posx = posx, posy = posy, reference = {}}
setmetatable(_tabledata[name].reference, { __index = referencetable })
end
drawimage("Header", 0, 50, table)
一切都很好,价值观有效,我们都很高兴..当参考表以这种方式改变它的价值时,问题就出现了
local data = { 123123, 545454, "heyou" } -- Data is sent from another script via a trigger
table = data
由于我没有通过索引(即:table[1] = 9999)来更新它,因此引用变量与真实的引用变量“不同步”,我希望你能理解 :)
编辑2:
好的,这是我的主要问题的自我工作示例
local maintable = { "Stack", "Overflow" }
local maintablecopy = {}
maintablecopy = maintable
print("maintable[1] = " ..maintable[1]) -- Prints Stack
print("maintable[2] = " ..maintable[2]) -- Prints Overflow
print("")
print("maintablecopy[1] = " ..maintablecopy[1]) -- Prints Stack
print("maintablecopy[2] = " ..maintablecopy[2]) -- Prints Overflow
print("")
print("Changing values..")
local newdata = { "Hello", "World" }
maintable = newdata
print("")
print("maintable[1] = " ..maintable[1]) -- Prints Hello
print("maintable[2] = " ..maintable[2]) -- Prints World
print("")
print("maintablecopy[1] = " ..maintablecopy[1]) -- Prints Stack -- PROBLEM
print("maintablecopy[2] = " ..maintablecopy[2]) -- Prints Overflow -- PROBLEM
print("Using setmetatable..")
maintable = { "Stack", "Overflow" }
maintablecopy = {}
setmetatable(maintablecopy, { __index = maintable })
print("maintable[1] = " ..maintable[1]) -- Prints Stack
print("maintable[2] = " ..maintable[2]) -- Prints Overflow
print("")
print("maintablecopy[1] = " ..maintablecopy[1]) -- Prints Stack
print("maintablecopy[2] = " ..maintablecopy[2]) -- Prints Overflow
print("")
print("Changing values..")
local newdata = { "Hello", "World" }
maintable = newdata
print("")
print("maintable[1] = " ..maintable[1]) -- Prints Hello
print("maintable[2] = " ..maintable[2]) -- Prints World
print("")
print("maintablecopy[1] = " ..maintablecopy[1]) -- Prints Stack -- PROBLEM
print("maintablecopy[2] = " ..maintablecopy[2]) -- Prints Overflow -- PROBLEM
为什么当变量更新时我不能直接将它指向表格?因为我有 20 个表要更新,这样做会更容易
local _dynamics = {}
local tbl1 = { "Hey", 8787 }
local tbl2 = { 123, "There" }
local tbl3 = { "You", 1111 }
function dynamicFunction(name, posx, posy, textsize, reference)
_dynamics[name] = { posx = posx, posy = posy, textsize = textsize, reference = reference }
end
dynamicFunction("first", 0, 0, 5, tbl1)
dynamicFunction("second", 0, 0, 5, tbl2)
dynamicFunction("third", 0, 0, 5, tbl3)
for key in pairs(_dynamics) do
local inf = _dynamics[key]
for i = 1, #inf.reference do
print(inf.reference[i])
if i == #inf.reference then
print("")
end
end
end
print("")
print("")
tbl1 = { "aaaaa", "bbbbbbbbbb" }
tbl2 = { "ccccccccccc", "ttttttttttt" }
tbl3 = { "rrrrrrr", "yyyyyyyyyyy" }
for key in pairs(_dynamics) do
local inf = _dynamics[key]
for i = 1, #inf.reference do
print(inf.reference[i])
if i == #inf.reference then
print("")
end
end
end
print("Values should get updated on the reference variable, but it doesn't.. this would save me to do a check for every single variable")
您可以在http://www.compileonline.com/execute_lua_online.php上运行它来了解我的意思。
对不起,如果它是一团糟,但我的英语不是最好的:D