0

有没有办法将表指向另一个表?例如:

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

4

1 回答 1

2

你想要元__index方法

local a = { name="Josh" }
local b = {}

print(a.name) --> Josh
print(b.name) --> nil

setmetatable(b,{__index=a})
print(b.name) --> Josh

a.name = "Gilligan"
print(a.name) --> Gilligan
print(b.name) --> Gilligan

-- but note! the shadow
b.name = "overridden"
print(a.name) --> Gilligan
print(b.name) --> overridden

b.name = nil
print(a.name) --> Gilligan
print(b.name) --> Gilligan

有关更多详细信息,我提供了我的这篇文章:http:
//phrogz.net/lua/LearningLua_ValuesAndMetatables.html


对 Edit2 的回应

让我总结一下您的一些代码的问题:

local maintablecopy = {}
maintablecopy = maintable

使用上面的代码创建一个表,设置maintablecopy为引用该表,然后在设置maintablecopy为引用另一个表时完全放弃它。这表明缺乏对变量如何工作的理解。

local newdata = { "Hello", "World" }
maintable = newdata

同样,您不是在“复制”newdatamaintable中,而是在更改变量以在此处引用同一个表。

maintable = { "Stack", "Overflow" }
maintablecopy = {}
setmetatable(maintablecopy, { __index = maintable })

-- …

local newdata = { "Hello", "World" }
maintable = newdata

再次,同样的问题。以下是一些更改代码的方法:

替换表格的内容

而不是maintable = newdata你可以这样做:

function copytable(from,to_table)
  -- erase all old keys
  for k,_ in pairs(to_table) do to_table[k] = nil end

  -- copy the new ones over
  for k,v in pairs(from) do to_table[k] = v end
end

local a = { name="Foo" }
local b = {}
copytable(a,b)
print(a.name == b.name) --> true

local c = { name="NEW" }
copytable(c,b)
print(c.name == b.name) --> true

但是,如果更改,这样做不会导致b更新。c

c.name = "EVEN NEWER"
print(c.name == b.name) --> false

更新 __index

local a = { name="Foo" }
local b = setmetatable({},{__index=a})
print(a.name == b.name) --> true

-- cause b to follow c now instead of a
local c = { name="NEW" }
getmetatable(b).__index = c
print(c.name == b.name) --> true

c.name = "EVEN NEWER"
print(c.name == b.name) --> true

一般来说,你需要退后一步,描述你试图解决的原始问题,而不是这个XY 问题

于 2014-11-23T04:09:41.297 回答